可能在Android中获得“所有者”联系信息?

我一直没有find直接的答案。 任何人都可以告诉我,如果有可能在Android应用程序中获取手机所有者的联系信息?

Solutions Collecting From Web of "可能在Android中获得“所有者”联系信息?"

我发现了一个非常简单的方法(从挖掘到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) 

和…..你完成了!

所以答案在技术上不是。 我迄今为止唯一获得所有者数据的方法是通过客户经理。 这是一个如何使用它的例子:

 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

我们必须做的:

1)获取用户同步帐号名(通常是谷歌邮箱)
2)从这个电子邮件的联系簿获取联系
3)从这个联系人获取联系人数据

甚至不是完美的,需要两个额外的权限 – 但至less工程。

这是代码,可能的代码更新可以在这里: 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(); } } } } 

冰淇淋三明治或以后,使用

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