在安卓系统中是否能够获取“所有者”的联系信息?

9

我在这个问题上一直没有找到一个明确的答案。有人能告诉我在Android应用程序中是否可以获取手机所有者的联系信息吗?


你是指电话号码吗?如果是的话,你可以使用getLine1Number(http://developer.android.com/reference/android/telephony/TelephonyManager.html#getLine1Number%28%29),尽管它不是100%可靠的。 - keyboardP
我正在寻找姓名和地址。 - wajiw
我也在研究这个问题,我的应用程序需要用户输入他们的姓名、电子邮件和电话号码,这些信息显然已经存储在手机中。我找不到我曾经阅读过的页面,但所有的方法都说获取这些信息的方式已经被弃用,并且出于安全原因已经从新的API中删除。你可以直接向用户询问并将数据保存在“SharedPreferences”中。 - dylan murphy
5个回答

13

我找到了一种非常简单的方法(从研究 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)

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


1
此方法仅支持Android 4.0(API 14)或更高版本。因此,如果您只关心在20-25%的设备上使用它,则可以适用。 - DanO
1
@Daniel,你的评论在你写它的时候广泛适用 - 但它只会变得更加流行。而且Daksh的代码运行得很好,只要你将其包围在 android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH 中。有足够多的搜索查询会聚焦于这个响应,我们应该为未来推广它! - Kenton Price
请注意,如果您没有在AndroidManifest.xml中添加<uses-permission android:name="android.permission.READ_PROFILE" />,则可能会遇到SecurityException异常。 - declension
在Android中更新“所有者”联系信息是否可行? - neeraj kirola
这需要正确的READ_CONTACTS权限。由于需要此权限的应用程序是可疑的,因此这不是一个好的解决方案。实际上,为了仅读取手机拥有者的姓名,不需要获得读取所有联系人的权限。 - Holger Jakobs
我理解的是否正确,Phone._ID字段将等同于个人资料联系人的ContactsContract.Contacts.CONTACT_ID? - Josh Hansen

12

所以从技术上讲答案是否定的。目前我发现获取所有者数据的唯一方式是通过账户管理器。以下是如何使用它的示例:

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中更新“所有者”联系信息是可能的吗? - neeraj kirola

6
我们需要做的事情:
1)获取用户同步帐户名称(通常是谷歌电子邮件) 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();
            }
        }
    }
}

从表中获取个人资料信息非常有用。是否可以更新个人资料信息? - neeraj kirola
@neerajkirola 如果您指的是更新用户联系人 - 是的,如果您有管理联系人的权限,那么这是可能的。这在其他问题中已经讨论过,例如http://stackoverflow.com/questions/10603187/modify-native-contact-programmatically - Jehy
在Android中更新“所有者”联系信息是可能的吗? - neeraj kirola
@neerajkirola 这只是用户联系人中的另一个联系人,为什么不呢。 - Jehy
非常感谢您一直回复我的查询……您是否可以将更新所有者个人资料(姓名、联系电话等)的代码放在这里?非常感谢!! - neeraj kirola
@neerajkirola 这个话题有点偏离主题,但你可以检查一下这段代码:https://dev59.com/Reo6XIcBkEYKwwoYQiS7 - Jehy

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();

1

从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()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接