我正在开发 android 应用程序,我需要在我的应用程序的特定联系人中添加自定义行。直到现在我能够在我的联系人中添加行但是当我单击该行时它不会打开我的应用程序而是显示类似这样的吐司:“未找到处理此操作的应用程序”。
我用谷歌搜索了很多,但没有运气。
这是我的代码:在特定联系人中添加自定义行的方法:
private void addContact(Account account, ContactDTO dto) {
Log.e("addContact", "dto : " + dto.displayName + " & dto : " + dto.contactID);
Log.e("addContact", "account.name : " + account.name + " & account.type : " + account.type);
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, dto.displayName);
operationList.add(builder.build());
builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
builder.withValueBackReference(Data.RAW_CONTACT_ID, 0);
builder.withValue(Data.MIMETYPE, getResources().getString(R.string.mimeType));
builder.withValue(Data.DATA1, dto.displayName);
builder.withValue(Data.DATA2, dto.phoneNo);
builder.withValue(Data.DATA3, "View App");
operationList.add(builder.build());
operationList.add(ContentProviderOperation.newUpdate(AggregationExceptions.CONTENT_URI)
.withValue(AggregationExceptions.TYPE, AggregationExceptions.TYPE_KEEP_TOGETHER)
.withValueBackReference(AggregationExceptions.RAW_CONTACT_ID1, 0)
.withValue(AggregationExceptions.RAW_CONTACT_ID2, dto.contactID)
.withValue(AggregationExceptions.CONTENT_ITEM_TYPE, getResources().getString(R.string.mimeType))
.build());
try {
mContentResolver.applyBatch(ContactsContract.AUTHORITY, operationList);
} catch (OperationApplicationException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
}
清单.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.demosample">
<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" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@drawable/ic_launcher"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter android:icon="@drawable/ic_launcher">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter android:icon="@drawable/ic_launcher">
<!--<action android:name="android.intent.action.VIEW" />-->
<action android:name="android.intent.action.VIEW" />
<action android:name="fm.last.android.sync.LOGIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="vnd.android.cursor.item/fm.last.android.sync.login" />
<!--<data android:host="ccc.in.demosample.mainactivity" />-->
<!--<data android:scheme="http" />-->
<!--<data android:pathPattern="/.*" />-->
<data android:mimeType="vnd.android.cursor.item/fm.last.android.sync.login" />
</intent-filter>
</activity>
<service
android:name=".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=".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>
</application>
</manifest>
联系人.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:mimeType="vnd.android.cursor.item/fm.last.android.sync.login"
android:summaryColumn="data2" />
</ContactsSource>
account_preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
</PreferenceScreen>
身份验证器.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="@string/ACCOUNT_TYPE"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:smallIcon="@drawable/ic_launcher" />
sync_contacts.xml
<?xml version="1.0" encoding="utf-8"?>
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="@string/ACCOUNT_TYPE"
android:contentAuthority="com.android.contacts"
android:supportsUploading="false" />
而已。请让我知道我哪里出错了。上面的代码将在没有应用程序图标的联系人中添加自定义行,通过单击该行,它将显示一个 toast 并且不会打开我的应用程序。