Basically what I need to achieve is very clear but I am having many doubts in my implementation. I went through R & D about this topic but didn't find satisfied answer. Hope you guys will understand me and help me further.
So here is screen shot for what I want to do:
Contact detail page
What I did till now:
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>
Here in intent-filter tag should I need to define mime-type
? If I define mime-type
as above my application is not being listed in Application list of device.
Permissions in 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>
Don't know much about this file.
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();
}
}
This is it. Let me know where I am doing wrong? Some doubts I faced:
Why we need to add Account in device Settings?
I can see that my code is only to add new record. How can I update specific contact to add custom row.
Till now with above code, my application is only listed in Accounts section in Settings menu. I want to add custom row in my contact list who are using my application.
Please help me with this scenario. Thanks in advance!