2011-06-03 41 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

相關問題