【问题标题】:Possible to get "Owner" contact info in Android?可以在 Android 中获取“所有者”联系信息吗?
【发布时间】:2011-09-07 12:29:26
【问题描述】:

我无法找到一个直接的答案。谁能告诉我是否可以在 Android 应用中获取手机所有者的联系信息?

【问题讨论】:

  • 你是指电话号码吗?如果是这样,您可以使用 getLine1Number (developer.android.com/reference/android/telephony/…),尽管它不是 100% 可靠的。
  • 我在找姓名和地址
  • 我也在研究这个,我的应用程序涉及用户输入他们的姓名、电子邮件和电话号码,这些显然都已经存储在手机中。我找不到我正在阅读的页面,但所有内容都表明,出于安全原因,所有获取该信息的方法都已/正在被弃用并从较新的 api 中删除。您可以随时向用户索取并将数据保存在SharedPreferences

标签: android android-contacts


【解决方案1】:

我找到了一个非常简单的方法(通过深入研究 4.1 消息应用程序得到它!)

光标的投影是

final String[] SELF_PROJECTION = new String[] { Phone._ID,Phone.DISPLAY_NAME, };

光标是:

Cursor cursor = activity.getContentResolver().query(Profile.CONTENT_URI, SELF_PROJECTION, null, null, null);

现在做一个简单的

cursor.moveToFirst():

然后通过

获取联系人ID
cursor.getString(0)

和联系人姓名通过

cursor.getString(1)

然后......你完成了!

【讨论】:

  • 此方法仅在 Android 4.0 (API 14) 或更高版本中受支持。因此,如果您只关心在 20-25% 的设备上使用它,那么它可以为您工作。
  • @Daniel,你的评论在你写它的时候大体上是正确的——但它只会变得更受欢迎。只要你用android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH 包围它,Daksh 的代码就很有效。足够多的搜索查询对这个响应进行了磨练,我们应该在未来推广它!
  • 请注意,您需要将<uses-permission android:name="android.permission.READ_PROFILE" /> 添加到您的AndroidManifest.xml 以免被SecurityException 击中
  • 是否可以在 Android 中更新“所有者”联系信息?
  • 这需要正确的 READ_CONTACTS。由于需要此权限的应用程序是可疑的,因此这不是一个好的解决方案。实际上,读取所有联系人的权限不一定是读取手机所有者姓名的必要条件。
【解决方案2】:

所以从技术上讲,答案是否定的。到目前为止,我发现获取所有者数据的唯一方法是通过客户经理。这是一个如何使用它的示例:

final AccountManager manager = AccountManager.get(this);
final Account[] accounts = manager.getAccountsByType("com.google");
final int size = accounts.length;
String[] names = new String[size];
for (int i = 0; i < size; i++) {
  names[i] = accounts[i].name;
}

欲了解更多信息,请参阅:http://code.google.com/p/google-api-java-client/wiki/AndroidAccountManager

【讨论】:

  • 是否可以在 Android 中更新“所有者”联系信息?
【解决方案3】:

我们要做的:

1) 获取用户同步账户名(通常是google email)
2) 使用此电子邮件从通讯录中获取联系
3) 从此联系人获取联系人数据

甚至不接近完美,需要两个额外的权限 - 但至少可以工作。

这是代码,可能的代码更新可以在这里: https://gist.github.com/3904299

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.util.Log;

public class OwnerInfo {
    // this class allows to get device information. It's done in two steps:
    // 1) get synchronization account email
    // 2) get contact data, associated with this email
    // by https://github.com/jehy
    //WARNING! You need to have permissions
    //
    //<uses-permission android:name="android.permission.READ_CONTACTS" />
    //<uses-permission android:name="android.permission.GET_ACCOUNTS" />
    //
    // in your AndroidManifest.xml for this code.

    public String id = null;
    public String email = null;
    public String phone = null;
    public String accountName = null;
    public String name = null;

    public OwnerInfo(Activity MainActivity) {
        final AccountManager manager = AccountManager.get(MainActivity);
        final Account[] accounts = manager.getAccountsByType("com.google");
        if (accounts[0].name != null) {
            accountName = accounts[0].name;

            ContentResolver cr = MainActivity.getContentResolver();
            Cursor emailCur = cr.query(
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Email.DATA + " = ?",
                    new String[] { accountName }, null);
            while (emailCur.moveToNext()) {
                id = emailCur
                        .getString(emailCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTACT_ID));
                email = emailCur
                        .getString(emailCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                String newName = emailCur
                        .getString(emailCur
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                if (name == null || newName.length() > name.length())
                    name = newName;

                Log.v("Got contacts", "ID " + id + " Email : " + email
                        + " Name : " + name);
            }

            emailCur.close();
            if (id != null) {

                // get the phone number
                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = ?", new String[] { id }, null);
                while (pCur.moveToNext()) {
                    phone = pCur
                            .getString(pCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    Log.v("Got contacts", "phone" + phone);
                }
                pCur.close();
            }
        }
    }
}

【讨论】:

  • 从表中获取个人资料信息很有用。是否可以更新个人资料信息?
  • @neerajkirola 如果您的意思是更新用户的联系人 - 是的,如果您有权管理联系人,则可以。在其他问题中讨论,例如stackoverflow.com/questions/10603187/…
  • 是否可以在 Android 中更新“所有者”联系信息?
  • @neerajkirola 它只是用户联系人中的另一个联系人,为什么不呢。
  • 感谢您不断回复我的问题...您能把代码放在这里更新所有者资料(姓名、联系电话等)非常感谢!!
【解决方案4】:

对于冰淇淋三明治或更高版本,使用

String[] columnNames = new String[] {Profile.DISPLAY_NAME, Profile.PHOTO_ID};

Cursor c = activity.getContentResolver().query(ContactsContract.Profile.CONTENT_URI, columnNames, null, null, null);
int count = c.getCount();
boolean b = c.moveToFirst();
int position = c.getPosition();
if (count == 1 && position == 0) {
    for (int j = 0; j < columnNames.length; j++) {
        String name = c.getString(0));
        long photoId = c.getLong(1));
    }
}
c.close();

【讨论】:

    【解决方案5】:

    从 API 23 及更高版本开始,您需要在清单中添加适当的权限,

    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
    

    然后您将能够检索用户信息,例如,

    String[] columnNames = new String[] {ContactsContract.Profile.DISPLAY_NAME, ContactsContract.Profile.PHOTO_ID};
    
    Cursor c = activity.getContentResolver().query(ContactsContract.Profile.CONTENT_URI, columnNames, null, null, null);
    int count = c.getCount();
    boolean b = c.moveToFirst();
    int position = c.getPosition();
    if (count == 1 && position == 0) {
        for (int j = 0; j < count; j++) {
            String name = c.getString(c.getColumnIndex(ContactsContract.Profile.DISPLAY_NAME));
            String photoID = c.getString(c.getColumnIndex(ContactsContract.Profile.PHOTO_ID));
            Log.i("MainActivity", "name: " + name);
            Log.i("MainActivity", "photoID: "+ photoID);
        }
    }
    c.close()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-17
      • 2011-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多