2011-06-03 57 views
7

我一直没有找到直接的答案。任何人都可以告诉我是否有可能在Android应用程序中获取手机所有者的联系信息?可能在Android中获取“所有者”联系信息?

+0

你的意思的电话号码?如果是这样,你可以使用getLine1Number(http://developer.android.com/reference/android/telephony/TelephonyManager.html#getLine1Number%28%29),尽管它不是100%可靠的。 – keyboardP 2011-06-03 02:30:58

+0

我正在寻找名称和地址 – wajiw 2011-06-03 02:33:53

+0

我也在看这个,我的应用程序涉及用户输入他们的姓名,电子邮件和电话号码,这些都显然存储在手机中。我无法找到我正在阅读的页面,但是所有内容都表示,出于安全原因,获取该信息的所有方法都被弃用,并从新的apis中删除。你可以随时询问用户并将数据保存在'SharedPreferences' – 2011-06-03 02:53:33

回答

11

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

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

+0

是否可以更新Android中的“所有者”联系信息? – 2015-12-18 06:00:01

12

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

,然后通过

cursor.getString(1) 

通过

cursor.getString(0) 

和联系人姓名获取联系ID和.....你完成了!

+1

此方法仅在Android 4.0(API 14)或更高版本中受支持。所以如果你只关心在20-25%的设备上使用它,那么它可以为你工作。 – DanO 2012-11-08 16:22:50

+1

@丹尼尔,你的评论在你写的时候大体上是真实的 - 但它只会变得更受欢迎。而且Daksh的代码可以工作,只要你用'android.os.Build.VERSION.SDK_INT> = android.os.Build包围它即可。VERSION_CODES.ICE_CREAM_SANDWICH'。足够的搜索查询在这个响应中磨合,我们应该为未来推广它! – 2012-12-08 03:49:03

+0

请注意,您需要为您的'AndroidManifest.xml'添加 '''以免受到'SecurityException'的攻击 – declension 2013-12-22 12:05:40

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(); 
      } 
     } 
    } 
} 
+0

从表中获取个人资料信息非常有用。是否有可能更新个人资料信息? – 2015-12-16 09:30:37

+0

@neerajkirola如果你的意思是更新用户的联系人 - 是的,如果你有权限管理联系人,这是可能的。它在其他问题中讨论,例如http://stackoverflow.com/questions/10603187/modify-native-contact-programmatically – Jehy 2015-12-17 08:24:58

+0

是否有可能在Android中更新“所有者”联系信息? – 2015-12-18 06:00:21

3

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

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

如何从配置文件中获取手机号码? – 2015-02-18 11:43:09

相关问题