Use Android Contacts app to edit a raw contact whi

2019-04-11 06:39发布

问题:

My app adds contact 'Bob' to the address book using a custom ContentProvider. In the Android Contacts app, Bob shows up just as any other (Google) contact. But when I edit Bob in the Contacts app, the data provided by my application is not editable. So far so good.

My question is: From within my app, is there a way to fire up the Contacts app, in a way that allows the user to edit the portion of Bob that belongs to my app?

I have tried to use the corresponding Intent, as described in this Android guide, but using the raw contact uri for Bob:

Uri rawUri = getRawContactUri("Bob");
Intent intent = new Intent(Intent.ACTION_EDIT, rawUri);
startActivityForResult(intent, EDIT_CONTACT_RESULT);

which brings up the Contacts app, but still without the possibility to edit Bob's data.

There are plenty of questions on SO covering how to open your app from within the Contacts app when a custom field is selected or how to fire ACTION_EDIT correctly.
But I did not find any statement - including a reference - if it is possible to use Contacts app to let the user edit custom raw contacts. Does anyone have a clue and preferably a reference?

回答1:

You need to add EditSchema to your contact.xml file and add meta-data, pointing to that file in SyncService section of your manifest file, like this:

<service
    android:name=".syncadapter.SyncService"
    android:exported="true">

    <intent-filter>
        <action android:name="android.content.SyncAdapter" />
    </intent-filter>

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

Here is example of EditSchema in official sources: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/4.2.2_r1/packages/apps/Contacts/tests/res/xml/test_basic_contacts.xml/