【问题标题】:Display the app icon if the contact is associated with the application in phone address book如果联系人与电话通讯录中的应用程序关联,则显示应用程序图标
【发布时间】:2014-02-11 11:49:49
【问题描述】:

我正在尝试显示与应用程序关联的电话号码的应用程序图标。

我尝试关注this 链接,但太难了。
有没有解决这个问题的库或任何简单的方法来解决这个问题?

例如,我们可以说联系人存在于 whatsapp、facebook、google ......在电话通讯录中。
同样,我想在这些 Messenger 应用程序旁边显示我的应用程序图标。

【问题讨论】:

  • 你找到解决方案了吗??如果是,请分享.....
  • @Rahul 不,我没有找到解决方案。如果你知道,欢迎你
  • 这两个链接帮助我解决了这个问题Part 1Part 2@Kartheek
  • 你好 Kartheek,如果你实现了,你能分享代码或想法吗?
  • @Rahul 我关注了这两个链接并面临我已发布here 的问题。该问题有一个开放的赏金,并且我已实现的完整代码已发布here

标签: android contacts accountmanager


【解决方案1】:

以下代码显示了一个可能的解决方案。调用 synchronizeContact 方法将导致在联系人应用程序中添加链接。 请注意,它还不是健壮的代码,但它显示了这个想法并且正在工作。 另请注意,以下两个 POJO 类特定于我的实现,对于联系人链接的工作不是必需的:PhoneNumber、ContactInfo。

MainActivity.java:

private void synchronizeContact(ContactInfo contactInfo)
{
    ContactsSyncAdapterService syncAdapter = new ContactsSyncAdapterService();
    Account account = new Account(contactInfo.getDisplayName(), getString(R.string.account_type)); //account_type = <yourpackage>.account
    PhoneNumber phoneNumber = contactInfo.getPhoneNumbers().get(0);
    syncAdapter.performSync(MainActivity.this, account, phoneNumber);
}

ContactsSyncAdapterService:

private static ContentResolver itsContentResolver = null;

public void performSync(Context context, Account account, PhoneNumber number)
{
    itsContentResolver = context.getContentResolver();
    addContact(account, account.name, account.name, number.getNumber());
}

private void addContact(Account account, String name, String username, String number)
{
    ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();

    ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);
    builder.withValue(RawContacts.ACCOUNT_NAME, account.name);
    builder.withValue(RawContacts.ACCOUNT_TYPE, account.type);
    builder.withValue(RawContacts.SYNC1, username);
    operationList.add(builder.build());

    builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
    builder.withValueBackReference(ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID, 0);
    builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
    builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name);
    operationList.add(builder.build());

    builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
    builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0);
    builder.withValue(ContactsContract.Data.MIMETYPE, "vnd.android.cursor.item/vnd.<yourpackage>.profile");
    builder.withValue(ContactsContract.Data.DATA1, username);
    builder.withValue(ContactsContract.Data.DATA2, number);
    operationList.add(builder.build());

    try
    {
        itsContentResolver.applyBatch(ContactsContract.AUTHORITY, operationList);
    }
    catch (OperationApplicationException e)
    {
        e.printStackTrace();
    }
    catch (RemoteException e)
    {
        e.printStackTrace();
    }
}

ProfileActivity(点击联系人应用链接时的意图类):

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.profile);

    Uri intentData = getIntent().getData();
    if (isNotEmpty(intentData))
    {
        Cursor cursor = managedQuery(intentData, null, null, null, null);
        if (cursor.moveToNext())
        {
            String username = cursor.getString(cursor.getColumnIndex("DATA1"));
            String number = cursor.getString(cursor.getColumnIndex("DATA2"));
            TextView view = (TextView) findViewById(R.id.profiletext);
            view.setText("<yourtext>");
            doSomething(getPhoneNumber(number));
        }
    }
    else
    {
        handleIntentWithoutData();
    }
}

private void doSomething(PhoneNumber phoneNumber)
{
    Uri uri = Uri.parse("tel:" + phoneNumber.getNumber());
    Intent intent = new Intent(Intent.ACTION_CALL, uri);
    startActivity(intent);
}

contacts.xml:

<?xml version="1.0" encoding="utf-8"?>
<ContactsSource xmlns:android="http://schemas.android.com/apk/res/android">
    <ContactsDataKind
        android:icon="@drawable/ic_launcher"
        android:mimeType="vnd.android.cursor.item/vnd.<yourpackage>.profile"
        android:summaryColumn="data2"
        android:detailColumn="data3"
        android:detailSocialSummary="true"
    />

身份验证器.xml:

<?xml version="1.0" encoding="utf-8"?>
<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
    android:accountType="<yourpackage>.account"
    android:icon="@drawable/ic_launcher"
    android:smallIcon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:accountPreferences="@xml/account_preferences"
/>

account_preferences.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
</PreferenceScreen>

sync_contacts.xml:

<?xml version="1.0" encoding="utf-8"?>
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentAuthority="com.android.contacts" 
    android:accountType="<yourpackage>.account"
    android:supportsUploading="false"
/>

电话号码:

private String number;

public String getNumber()
{
    return number;
}

public void setNumber(String number)
{
    this.number = number;
}

联系方式:

private List<PhoneNumber> itsPhoneNumbers = new ArrayList<PhoneNumber>();

public void setDisplayName(String displayName)
{
    this.itsDisplayName = displayName;
}

public String getDisplayName()
{
    return itsDisplayName;
}

public void addPhoneNumber(PhoneNumber phoneNumber)
{
    this.itsPhoneNumbers.add(phoneNumber);
}

public List<PhoneNumber> getPhoneNumbers()
{
    return itsPhoneNumbers;
}

【讨论】:

  • 我必须在哪里定义 ProfileActivity ?
  • 在 AndroidManifest.XML 中:
  • 我是否应该始终添加新联系人以显示我的应用程序图标。如果已经存在,我不能更新联系人吗?
  • 要回答 Nav,应该可以将应用程序图标添加到现有联系人。然而,上面的代码还不是健壮的代码,还不支持该功能。
  • 到目前为止添加。如何移除应用图标?
猜你喜欢
  • 2019-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多