How to add a phone number to a contact programmatically in android studio
Create a ContactsUI.java Activity in android studio.
1.ContactsUI.java:
package com.contact;
import java.util.ArrayList;
import android.accounts.AccountManager;
import android.accounts.AuthenticatorDescription;
import android.app.Activity;
import android.content.ContentProviderOperation;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.widget.Toast;
public class ContactsUI extends Activity {
private static final String ACCOUNT_NAME = "darwinian";
private static String LOG_TAG;
private AccountManager accountManager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LOG_TAG = getString(R.string.app_name);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
accountManager = AccountManager.get(this);
addContact();
}
private void addContact() {
String name = "Jon Smith";
String homePhone = "416-555-5555";
String workPhone = "416-555-6666";
String email = "jon@jonsmith.domain";
// Use new-style batch operations: Build List of ops then call applyBatch
try {
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
AuthenticatorDescription[] types = accountManager.getAuthenticatorTypes();
ops.add(ContentProviderOperation.newInsert(
ContactsContract.RawContacts.CONTENT_URI).withValue(
ContactsContract.RawContacts.ACCOUNT_TYPE, types[0].type)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, ACCOUNT_NAME)
.build());
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,name)
.build());
ops.add(ContentProviderOperation.newInsert(
ContactsContract.Data.CONTENT_URI).withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,
homePhone).withValue(
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_HOME)
.build());
ops.add(ContentProviderOperation.newInsert(
ContactsContract.Data.CONTENT_URI).withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,
workPhone).withValue(
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_WORK)
.build());
ops.add(ContentProviderOperation.newInsert(
ContactsContract.Data.CONTENT_URI).withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, email)
.withValue(ContactsContract.CommonDataKinds.Email.TYPE,
ContactsContract.CommonDataKinds.Email.TYPE_HOME)
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
Toast.makeText(this, getString(R.string.addContactSuccess),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this, getString(R.string.addContactFailure),
Toast.LENGTH_LONG).show();
Log.e(LOG_TAG, getString(R.string.addContactFailure), e);
}
}
}
2.activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:gravity="center_horizontal"
/>
</LinearLayout>
3.strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Add Contacts Data</string>
<string name="app_name">HotContacts</string>
<string name="addAccounrSuccess">Account created</string>
<string name="addAccounrFailure">Account creation failed</string>
<string name="addContactSuccess">Contact Inserted</string>
<string name="addContactFailure">Contact Insertion failed</string>
</resources>
4.AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.contact">
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"></uses-permission>
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ContactsUI">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
5.styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
6.Output:
No comments:
Post a Comment