在电话簿中添加自定义联系人行,如Whatsapp

时间:2017-12-06 06:01:00

标签: android android-contacts account phonebook

基本上我需要达到的目标非常明确,但我在实施过程中有很多疑问。我经历了R& D关于这个话题,但没有找到满意的答案。希望你们能理解我并进一步帮助我。

所以这是我想要做的屏幕截图:

联络详情页面

image

到目前为止我做了什么:

的Manifest.xml

        <activity
            android:name=".AddCardActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <!--<data android:mimeType="vnd.android.cursor.item/vnd.ccc.in.businesscard" />-->
            </intent-filter>
        </activity>

        <service
            android:name=".customcontact.AccountAuthenticatorService"
            android:exported="true"
            android:process=":auth">
            <intent-filter>
                <action android:name="android.accounts.AccountAuthenticator" />
            </intent-filter>
            <meta-data
                android:name="android.accounts.AccountAuthenticator"
                android:resource="@xml/authenticator" />
        </service>

        <service
            android:name=".customcontact.ContactsSyncAdapterService"
            android:exported="true"
            android:process=":contacts">
            <intent-filter>
                <action android:name="android.content.SyncAdapter" />
            </intent-filter>

            <meta-data
                android:name="android.content.SyncAdapter"
                android:resource="@xml/sync_contacts" />
            <meta-data
                android:name="android.provider.CONTACTS_STRUCTURE"
                android:resource="@xml/contacts" />
        </service>

在intent-filter标签中我应该定义mime-type吗?如果我如上所述定义mime-type,则我的应用程序未列在设备的应用程序列表中。

Manifest.XML中的权限:

<uses-permission
        android:name="android.permission.AUTHENTICATE_ACCOUNTS"
        android:maxSdkVersion="22" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
    <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />

authenticator.xml

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

account_preferences.xml

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

    <!--<PreferenceScreen-->
        <!--android:key="account_settings"-->
        <!--android:title="Account Settings"-->
        <!--android:summary="Sync frequency, notifications, etc.">-->
        <!--<intent-->
            <!--android:action="fm.last.android.activity.Preferences.ACCOUNT_SETUP"-->
            <!--android:targetPackage="fm.last.android"-->
            <!--android:targetClass="fm.last.android.activity.Preferences" />-->
    <!--</PreferenceScreen>-->
</PreferenceScreen>

对此文件不太了解。

sync_contacts.xml

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

contacts.xml

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

AddCardActivity.java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_card);

        addAccount("MyNewServiceNumber", "123456");
    }



private void addAccount(String userName, String password) {
        Account account = new Account(userName, getString(R.string.ACCOUNT_TYPE));
        AccountManager am = AccountManager.get(this);
        Bundle bundle = new Bundle();
        bundle.putString("Mobile", "+911111111111");
        boolean accountCreated = am.addAccountExplicitly(account, password, bundle);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            if (accountCreated) {  //Pass the new account back to the account manager
                AccountAuthenticatorResponse response = extras.getParcelable(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);
                Bundle result = new Bundle();
                result.putString(AccountManager.KEY_ACCOUNT_NAME, userName);
                result.putString(AccountManager.KEY_ACCOUNT_TYPE, getString(R.string.ACCOUNT_TYPE));
                response.onResult(result);
            }
        }
        addContactTag(account,"+911111111111");
        Toast.makeText(getBaseContext(), "Account added", Toast.LENGTH_SHORT).show();
    }

 public void addContactTag(Account account, String number) {

        ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();

        //  Create our RawContact
        ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI);
        builder.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, account.name);
        builder.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, account.type);
        operationList.add(builder.build());

        //  Create a Data record of common type 'Phone' for our RawContact
        builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
        builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0);
        builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
        builder.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number);
        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.ccc.in.businesscard");
        builder.withValue(ContactsContract.Data.DATA1, number);
        builder.withValue(ContactsContract.Data.DATA2, "ourservice user");
        builder.withValue(ContactsContract.Data.DATA3, "Go to ourservice profile");
        operationList.add(builder.build());


        try {
            ContentResolver mContentResolver = getContentResolver();
            mContentResolver.applyBatch(ContactsContract.AUTHORITY, operationList);
            Log.d("addContactTag", "addContact batch applied");
            Toast.makeText(getBaseContext(), "Contact added", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Log.e("addContactTag", "Something went wrong during creation! " + e);
            e.printStackTrace();
        }
    }

就是这样。让我知道我做错了什么?我面临一些疑问:

  1. 为什么我们需要在设备设置中添加帐户?

  2. 我可以看到我的代码只是添加新记录。如何更新特定联系人以添加自定义行。

  3. 现在使用上面的代码,我的应用程序仅列在“设置”菜单的“帐户”部分中。我想在我的联系人列表中添加使用我的应用程序的自定义行。

    请帮我解决这个问题。提前谢谢!

0 个答案:

没有答案