【问题标题】:Read all contacts (including imported from facebook, etc)读取所有联系人(包括从 facebook 导入等)
【发布时间】:2011-10-30 18:10:09
【问题描述】:

根据此示例:http://developer.android.com/resources/samples/ContactManager/index.html 我写了一个简单的函数来获取所有联系人:

 Uri uri = ContactsContract.Contacts.CONTENT_URI;
    String[] projection = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME
    };
    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" +
            (mShowInvisible ? "0" : "1") + "'";
    String[] selectionArgs = null;
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

它运行良好,但不返回使用 Facebook 应用程序导入的联系人(但它们在联系人应用程序中可见)。关于如何读取联系人应用程序中的所有联系人的任何想法?

谢谢

【问题讨论】:

    标签: android


    【解决方案1】:

    我认为您需要查看RawContacts 以了解该信息。

    这是 Android Contacts 文档所说的:

    RawContacts 表中的一行代表一组 Data 和其他 描述一个人并与单个联系人相关联的信息 资源。例如,一行可能定义与一个关联的数据 个人的 Google 或 Exchange 帐户或 Facebook 朋友。更多 信息,请参阅 ContactsContract.RawContacts。

    RawContacts 文档说:

    读取原始联系人以及所有相关数据的最佳方式 使用 ContactsContract.RawContacts.Entity 目录。 如果原始联系人有数据行,实体游标将包含一行 对于每个数据行。如果原始联系人没有数据行,则光标 仍将包含一行原始联系人级别信息。

    那里还有一些示例代码:

     Uri rawContactUri = 
       ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);
    
     Uri entityUri = 
       Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);
    
     Cursor c = 
       getContentResolver().query(entityUri,
                                  new String[]{RawContacts.SOURCE_ID, 
                                               Entity.DATA_ID, 
                                               Entity.MIMETYPE, 
                                               Entity.DATA1},
                                  null, 
                                  null, 
                                  null);
     try 
     {
         while (c.moveToNext()) 
         {
             String sourceId = c.getString(0);
    
             if (!c.isNull(1)) 
             {
                 String mimeType = c.getString(2);
                 String data = c.getString(3);
                 ...
             }
         }
     } 
     finally 
     {
         c.close();
     }
    

    【讨论】:

    • 什么是 rawContactId 以及在哪里获得它
    • 这根本没有帮助,因为问题首先是从提供商那里获取联系人。这个答案让你无处可去……
    【解决方案2】:

    在很多线程中提到 FaceBook 联系人无法被第三方应用程序读取,只有系统应用程序或活动可以读取。所以在任何人可以通过长按创建的任何 android 手机“DirectDial”和“DirectMessage”上在主屏幕上可以阅读 facebook 联系人。因此,我深入研究了 Android 源代码,以找到可以阅读 facebook 联系人的内容。

    我的发现。

    要为 'DirectDial' 、 'DirectMessage' 和 ContactShortcut 创建快捷方式,它将启动 ContactSelectionActivity ,该活动将返回 Intent 作为具有联系信息 'name' 'number' 和 'icon' 的结果。如果有人在这里挖掘什么是相同的链接

    https://github.com/android/platform_packages_apps_contacts/blob/master/src/com/android/contacts/activities/ContactSelectionActivity.java

    那么如何从您的代码中启动“DirectDial”或“DirectMessage” 我查看AndroidManifest.xml

    https://github.com/android/platform_packages_apps_contacts/blob/master/AndroidManifest.xml

          <activity-alias android:name="alias.DialShortcut"
            android:targetActivity=".activities.ContactSelectionActivity"
            android:label="@string/shortcutDialContact"
            android:icon="@mipmap/ic_launcher_shortcut_directdial"
            android:enabled="@*android:bool/config_voice_capable">
    
            <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.CAR_MODE" />
            </intent-filter>
    
        </activity-alias>
    
        <activity-alias android:name="alias.MessageShortcut"
            android:targetActivity=".activities.ContactSelectionActivity"
            android:label="@string/shortcutMessageContact"
            android:icon="@mipmap/ic_launcher_shortcut_directmessage"
            android:enabled="@*android:bool/config_voice_capable">
    
            <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
    
        </activity-alias>
    

    他们在这里创建 ContactSelectionActivity 的别名来启动“DirectDial”或“DirectMessage”

    所以这里是启动直接拨号或消息的代码

      private boolean launchContactSelector() {
        ComponentName distantActivity;
        if (Constants.PHONE_CALL)  //check what you want to call
            distantActivity = new ComponentName("com.android.contacts", "alias.DialShortcut");
        else {
            distantActivity = new ComponentName("com.android.contacts", "alias.MessageShortcut");
        }
        Intent intent = new Intent();
        intent.setComponent(distantActivity);
        intent.setAction(Intent.ACTION_PICK);
        intent.setAction(Intent.ACTION_CREATE_SHORTCUT);
        startActivityForResult(intent, selector);
        return true;
    
    }
    

    一旦选择了联系人

       protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK) return;
        Intent intent = new Intent();
        intent = ((Intent) data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT));
        switch (requestCode) {
        case Constants.PHONE_CALL: {
            intent.setAction(Intent.ACTION_CALL);
            break;
        }
        case Constants.MESSAGE: {
            intent.setAction(Intent.ACTION_SENDTO);
            break;
        }
        }
        String number = intent.getData().toString(); //It will have the phone number 
    
        String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); //Contact name 
        ImageView image = new ImageView() //Contact image 
        image.setImageBitmap((Bitmap) data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON));
    
        // Must need to set the Intent.ACTION_SENDTO or Intent.ACTION_CALL  because default          permission sent is CALL_PRIVILEGED which is for system Activty you .    
    
       startActivity(data);
    
        }
    

    因此可以在您的应用程序中读取 facebook 联系人 现在将信息存储在某个地方以供将来使用。

    我将在我实际使用它的地方完成我的开源应用程序的链接。

    【讨论】:

      猜你喜欢
      • 2011-08-31
      • 1970-01-01
      • 2020-12-02
      • 1970-01-01
      • 2012-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多