5

基本上我需要实现的目标非常明确,但我对我的实施有很多疑问。我对这个话题进行了研发,但没有找到满意的答案。希望你们能理解我并进一步帮助我。

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

联系方式页面

图片

我到目前为止所做的:

清单.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>

我应该在意图过滤器标签中定义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" />

身份验证器.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" />

联系人.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. 我可以看到我的代码只是添加新记录。如何更新特定联系人以添加自定义行。

到目前为止,使用上述代码,我的应用程序仅列在“设置”菜单的“帐户”部分中。我想在使用我的应用程序的联系人列表中添加自定义行。

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

4

0 回答 0