整体思路
需要在系统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 = 1;
+ private static final String INSERT_PRESET_NAME[] = {"Lyf Care"};
+ private static final String INSERT_PRESET_NUMBER[] = {"18008909999"};
+ private static final String INSERT_PRESET_EMAIL[] = {"care@jiolyf.com"};
+
+ 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 = 0; 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 != -1) {
+ 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", 1);
+ 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 = 0; 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 -1;
+ }
+ for (int i = 0; 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 -1;
+ }
+}
- 当然还需要相关的权限申请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"
@@ -229,5 +231,6 @@
<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", 1);
对于M上是没有效果的。