博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
手机中预置联系人(姓名,号码,头像)
阅读量:6226 次
发布时间:2019-06-21

本文共 5094 字,大约阅读时间需要 16 分钟。

此方法比较适合预置联系人的数目不是特别多的情况

【注意】如果您不需要限制预置联系人的删除/编辑操作, 去掉第三步”新增函数“

 中的语句: contactvalues.put(RawContacts.IS_SDN_CONTACT, -1);

File:packages/apps/Contacts/src/com/mediatek/contacts/simservice/SIMProcessorService.java

1.引入包

+import java.io.ByteArrayOutputStream;+import java.util.ArrayList;+import com.android.contacts.R;+import android.content.OperationApplicationException;+import android.database.Cursor;+import android.graphics.Bitmap;+import android.graphics.BitmapFactory;+import android.net.Uri;+import android.os.RemoteException;+import android.provider.ContactsContract.PhoneLookup;+import android.content.ContentProviderOperation;+import android.provider.ContactsContract.RawContacts+import android.content.ContentValues;+import android.provider.ContactsContract;+import android.provider.ContactsContract.Data;+import android.provider.ContactsContract.CommonDataKinds.Phone;+import android.provider.ContactsContract.CommonDataKinds.StructuredName;+import android.provider.ContactsContract.CommonDataKinds.Photo;+import com.android.contacts.common.model.account.AccountType;

2.增加变量

private static boolean sIsRunningNumberCheck = false;private static final int INSERT_PRESET_NUMBER_COUNT = xxx;           //预置联系人的个数private static final String   INSERT_PRESET_NAME[] = {"xxx1","xxx2",...};  //各预置联系人的姓名private static final String   INSERT_PRESET_NUMBER[] = {"xxx1","xxx2",...};  //各预置联系人的号码private static final int INSERT_PRESET_PHOTO[] = {R.drawable.contacts_photo,....}//各预置联系人头像
3.增加函数(将预置联系人信息写入数据库中)

private void importDefaultReadonlyContact() {new Thread(new Runnable() {@Overridepublic void run() {Log.i(TAG, "isRunningNumberCheck before: " + sIsRunningNumberCheck);if (sIsRunningNumberCheck) {return;}sIsRunningNumberCheck = true;             for(int i = 0;i < INSERT_PRESET_NUMBER_COUNT; i++)             {Log.i(TAG, "isRunningNumberCheck after: " + sIsRunningNumberCheck);Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(INSERT_PRESET_NUMBER[i]));Log.i(TAG, "getContactInfoByPhoneNumbers(), uri = " + uri);Cursor contactCursor = getContentResolver().query(uri, new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup.PHOTO_ID}, null, null, null);try {if (contactCursor != null && contactCursor.getCount() > 0) {return;} else {final ArrayList
operationList = newArrayList
();ContentProviderOperation.Builder builder = ContentProviderOperation                       .newInsert(RawContacts.CONTENT_URI);ContentValues contactvalues = new ContentValues();contactvalues.put(RawContacts.ACCOUNT_NAME,AccountType.ACCOUNT_NAME_LOCAL_PHONE);contactvalues.put(RawContacts.ACCOUNT_TYPE,AccountType.ACCOUNT_TYPE_LOCAL_PHONE);contactvalues.put(RawContacts.INDICATE_PHONE_SIM,       ContactsContract.RawContacts.INDICATE_PHONE);contactvalues.put(RawContacts.IS_SDN_CONTACT, -1);builder.withValues(contactvalues);builder.withValue(RawContacts.AGGREGATION_MODE,RawContacts.AGGREGATION_MODE_DISABLED);operationList.add(builder.build());builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);builder.withValueBackReference(Phone.RAW_CONTACT_ID, 0);builder.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);builder.withValue(Phone.TYPE, Phone.TYPE_MOBILE);builder.withValue(Phone.NUMBER, INSERT_PRESET_NUMBER[i]);builder.withValue(Data.IS_PRIMARY, 1);operationList.add(builder.build());builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);builder.withValueBackReference(StructuredName.RAW_CONTACT_ID, 0);builder.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);builder.withValue(StructuredName.DISPLAY_NAME, INSERT_PRESET_NAME[i]);operationList.add(builder.build());builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);builder.withValueBackReference(Photo.RAW_CONTACT_ID,0);builder.withValue(Data.MIMETYPE,Photo.CONTENT_ITEM_TYPE);Bitmap contactPhoto = BitmapFactory.decodeResource(getResources(),INSERT_PRESET_PHOTO[i]);ByteArrayOutputStream baos = new ByteArrayOutputStream();contactPhoto.compress(Bitmap.CompressFormat.PNG, 100, baos);byte[] photo = baos.toByteArray();builder.withValue(Photo.PHOTO, photo);operationList.add(builder.build());try {getContentResolver().applyBatch(ContactsContract.AUTHORITY, operationList);} catch (RemoteException e) {Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));} catch (OperationApplicationException e) {Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));}}         } finally {// when this service start,but the contactsprovider has not been started yet.// the contactCursor perhaps null, but not always.(first load will weekup the provider)// so add null block to avoid nullpointerexceptionif (contactCursor != null) {contactCursor.close();}}             }//forLog.i(TAG, "isRunningNumberCheck insert: " + sIsRunningNumberCheck);sIsRunningNumberCheck = false;}}).start();}
4.onStart中调用这个函数:

@Override     public void onStart(Intent intent, int startId) {         super.onStart(intent, startId);+            importDefaultReadonlyContact();         processIntent(intent);     }

参考文档:

MTK FAQ

转载于:https://www.cnblogs.com/chengliu/p/4130634.html

你可能感兴趣的文章
神经网络和深度学习-第二周神经网络基础-第二节:Logistic回归
查看>>
Myeclipse代码提示及如何设置自动提示
查看>>
c/c++中保留两位有效数字
查看>>
ElasticSearch 2 (32) - 信息聚合系列之范围限定
查看>>
VS2010远程调试C#程序
查看>>
[MicroPython]TurniBit开发板DIY自动窗帘模拟系统
查看>>
由String类的Split方法所遇到的两个问题
查看>>
Python3.4 12306 2015年3月验证码识别
查看>>
从Handler.post(Runnable r)再一次梳理Android的消息机制(以及handler的内存泄露)
查看>>
windows查看端口占用
查看>>
Yii用ajax实现无刷新检索更新CListView数据
查看>>
JDBC的事务
查看>>
Io流的概述
查看>>
App 卸载记录
查看>>
JavaScript变量和作用域
查看>>
开源SIP服务器加密软件NethidPro升级
查看>>
《别做正常的傻瓜》的一些读书心得
查看>>
作业:实现简单的shell sed替换功能和修改haproxy配置文件
查看>>
Altium 拼板方法以及 注意的 地方
查看>>
PMP考试的过与只是
查看>>