整體思路
需要在系統contacts中預置一個特定的聯系人,我們這裡采用的大體思路是:android啟動後會先啟動 launch ,是以我們就在launch裡面來向contacts中插入我們需要預置的資訊。
具體實作流程
- LINUX/android/packages/apps/Trebuchet/src/com/android/launcher3/Launcher.java 在onCreate 方法裡面加入以下代碼,啟動一個service
super.onCreate(savedInstanceState);
+ startService(new Intent(this, ImportDefaultContacts.class));
initializeDynamicGrid(false);
mHideIconLabels = SettingsProvider.getBoolean(this,
- 再來看看這個service LINUX/android/packages/apps/Trebuchet/src/com /android/launcher3/ImportDefaultContacts.java
+package com.android.launcher3;
+
+import android.app.IntentService;
+import android.content.ContentUris;
+import android.content.ContentValues;
+import android.content.Context;
+import android.content.Intent;
+import android.database.Cursor;
+import android.net.Uri;
+import android.provider.ContactsContract;
+import android.provider.ContactsContract.CommonDataKinds.Email;
+import android.provider.ContactsContract.CommonDataKinds.Phone;
+import android.provider.ContactsContract.CommonDataKinds.StructuredName;
+import android.provider.ContactsContract.RawContacts;
+import android.provider.ContactsContract.RawContacts.Data;
+import android.util.Log;
+
+public class ImportDefaultContacts extends IntentService {
+ private static final String TAG = "ImportDefaultContacts";
+ private static final int INSERT_PRESET_NUMBER_COUNT = ;
+ private static final String INSERT_PRESET_NAME[] = {"Lyf Care"};
+ private static final String INSERT_PRESET_NUMBER[] = {"18008909999"};
+ private static final String INSERT_PRESET_EMAIL[] = {"[email protected]"};
+
+ public ImportDefaultContacts() {
+ super("ImportDefaultContacts");
+ }
+
+ @Override
+ protected void onHandleIntent(Intent intent) {
+ if((INSERT_PRESET_NAME.length < INSERT_PRESET_NUMBER_COUNT) || (INSERT_PRESET_NUMBER.length < INSERT_PRESET_NUMBER_COUNT)
+ || INSERT_PRESET_EMAIL.length < INSERT_PRESET_NUMBER_COUNT)
+ return;
+ for (int i = ; i < INSERT_PRESET_NUMBER_COUNT; i++) {
+ String oldName = getContactNameByPhoneNumber(getApplicationContext(), INSERT_PRESET_NUMBER[i]);
+ if ((oldName == null) || !(oldName.equals(INSERT_PRESET_NAME[i]))) {
+ Log.i(TAG,"No_Contacts_AddDefaultContact");
+ AddContact(getApplicationContext(), INSERT_PRESET_NAME[i], INSERT_PRESET_NUMBER[i], INSERT_PRESET_EMAIL[i]);
+ } else if (oldName.equals(INSERT_PRESET_NAME[i])) {
+ Log.i(TAG,"Update_Default_Contacts");
+ long id = getContactRawIdByPhoneNumber(getApplicationContext(), INSERT_PRESET_NUMBER[i]);
+ if (id != -) {
+ ChangeContact(getApplicationContext(), INSERT_PRESET_NAME[i], INSERT_PRESET_NUMBER[i], INSERT_PRESET_EMAIL[i], "" + id);
+ }
+ }
+ }
+ }
+ }
+ public static void AddContact(Context context, String name, String number , String email)
+ {
+ ContentValues values = new ContentValues();
+ values.put("raw_contact_is_read_only", );
+ Uri rawContactUri = context.getContentResolver().insert(RawContacts.CONTENT_URI, values);
+ long rawContactId = ContentUris.parseId(rawContactUri);
+ values.clear();
+ values.put(Data.RAW_CONTACT_ID, rawContactId);
+ values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
+ values.put(StructuredName.GIVEN_NAME, name);
+ context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
+
+ values.clear();
+ values.put(Data.RAW_CONTACT_ID, rawContactId);
+ values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
+ values.put(Phone.NUMBER, number);
+ values.put(Phone.TYPE, Phone.TYPE_MOBILE);
+ context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
+
+ values.clear();
+ values.put(Data.RAW_CONTACT_ID, rawContactId);
+ values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
+ values.put(Email.DATA, email);
+ values.put(Email.TYPE, Email.TYPE_WORK);
+ context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
+ }
+
+ public static void ChangeContact(Context context, String name, String number, String email, String ContactId)
+ {
+ ContentValues values = new ContentValues();
+
+ values.put(StructuredName.GIVEN_NAME, name);
+ context.getContentResolver().update(ContactsContract.Data.CONTENT_URI,
+ values,
+ Data.RAW_CONTACT_ID + "=? and " + Data.MIMETYPE + "=?",
+ new String[] { ContactId, StructuredName.CONTENT_ITEM_TYPE });
+
+ values.clear();
+ values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, number);
+ context.getContentResolver().update(ContactsContract.Data.CONTENT_URI,
+ values,
+ Data.RAW_CONTACT_ID + "=? and " + Data.MIMETYPE + "=?",
+ new String[] { ContactId, Phone.CONTENT_ITEM_TYPE});
+
+
+ values.clear();
+ values.put(Email.DATA, email);
+ context.getContentResolver().update(ContactsContract.Data.CONTENT_URI,
+ values,
+ Data.RAW_CONTACT_ID + "=? and " + Data.MIMETYPE + "=?",
+ new String[] { ContactId, Email.CONTENT_ITEM_TYPE});
+ }
+
+ public static String getContactNameByPhoneNumber(Context context, String address) {
+ String[] projection = { ContactsContract.PhoneLookup.DISPLAY_NAME,
+ ContactsContract.CommonDataKinds.Phone.NUMBER };
+
+ Cursor cursor = context.getContentResolver().query(
+ ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
+ projection, // Which columns to return.
+ ContactsContract.CommonDataKinds.Phone.NUMBER + " = '"
+ + address + "'", // WHERE clause.
+ null, // WHERE clause value substitution
+ null); // Sort order.
+
+ if (cursor == null) {
+ return null;
+ }
+ for (int i = ; i < cursor.getCount(); i++) {
+ cursor.moveToPosition(i);
+ int nameFieldColumnIndex = cursor
+ .getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
+ String name = cursor.getString(nameFieldColumnIndex);
+ return name;
+ }
+ return null;
+ }
+
+ public static long getContactRawIdByPhoneNumber(Context context, String number) {
+ String[] projection = { ContactsContract.PhoneLookup.NAME_RAW_CONTACT_ID,
+ ContactsContract.CommonDataKinds.Phone.NUMBER };
+
+ Cursor cursor = context.getContentResolver().query(
+ ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
+ projection, // Which columns to return.
+ ContactsContract.CommonDataKinds.Phone.NUMBER + " = '"
+ + number + "'", // WHERE clause.
+ null, // WHERE clause value substitution
+ null); // Sort order.
+
+ if (cursor == null) {
+ return -;
+ }
+ for (int i = ; i < cursor.getCount(); i++) {
+ cursor.moveToPosition(i);
+ int idFieldColumnIndex = cursor
+ .getColumnIndex(ContactsContract.PhoneLookup.NAME_RAW_CONTACT_ID);
+ long id = cursor.getLong(idFieldColumnIndex);
+ return id;
+ }
+ return -;
+ }
+}
- 當然還需要相關的權限申請LINUX/android/packages/apps /Trebuchet/AndroidManifest.xml
<uses-permission android:name="cyanogenmod.permission.PROTECTED_APP" />
+ <uses-permission android:name="android.permission.READ_CONTACTS" />
+ <uses-permission android:name="android.permission.WRITE_CONTACTS" />
<application
android:name="com.android.launcher3.LauncherApplication"
@@ -, +, @@
<meta-data android:name="android.nfc.disable_beam_default"
android:value="true" />
+ <service android:name="com.android.launcher3.ImportDefaultContacts"></service>
</application>
說明
上面的代碼比較容易了解,都是對contacts資料庫的操作,這裡需要說明的是,android M 上面資料庫預設沒有了raw_contact_is_read_only這個列了,是以上面的
values.put("raw_contact_is_read_only", );
對于M上是沒有效果的。