显示应用程序图标如果联系人与电话地址簿中的应用程序相关联 [英] Display the app icon if the contact is associated with the application in phone address book

查看:174
本文介绍了显示应用程序图标如果联系人与电话地址簿中的应用程序相关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以显示它与应用程序相关联的电话号码的应用程序图标。

I am trying to display the application icon for the phone number which is associated with the application.

我试图按照链接,但实在是太难了。
是否有任何库这个或任何简单的方法来解决这个问题?

I tried to follow this link but it is too difficult.
Is there any library for this or any easy way to solve this problem?

例如,我们可以说接触是present在WhatsApp的,Facebook,谷歌,......在电话通讯录。
同样地,我想显示我的应用程序图标这些通讯应用程序的旁边。

For example, we can say the contact is present in whatsapp, facebook, google, ... in phone address book.
Similarly I want to display my application icon beside these messenger applications.

推荐答案

以下code显示了一个可能的解决方案。调用synchronizeContact方法将导致在添加联系人应用程序的链接。 请注意这还不是强大的code,但它显示的思想和工作正常。 注意也以下两个POJO类特定于我的实现,而不是必需的接触连接的工作:联系号码,CONTACTINFO

The following code shows a possible solution. Calling the synchronizeContact method will lead to adding the link in the contact app. Note it is not yet robust code but it shows the idea and is working. Note also the following two POJO classes are specific to my implementation and not essential to the working of the contact linking: PhoneNumber, ContactInfo.

MainActivity.java:

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:

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(攻联系人应用程序链接的时候类的意图):

ProfileActivity (class for the intent when tapping the contact app link):

@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:

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"
    />

authenticator.xml:

authenticator.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:

account_preferences.xml:

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

sync_contacts.xml:

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;
}

CONTACTINFO:

ContactInfo:

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;
}

这篇关于显示应用程序图标如果联系人与电话地址簿中的应用程序相关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆