这个功能我看了不少的资料,但总是删除不成功,我是在mtk 6.0上进行开发的。首先是通过"content://icc/adn/"来进行删除的,我发现这个根本就不行,

 public void deleteSIMContact(String name, String number) {String where = "tag='" + name + "'";where += " AND number='" + number + "'";int delete = context.getContentResolver().delete(uri, where, null);Log.d(TAG, "delete =" + delete);}

通过这个来查看,发现是删除了,

 public static void querySIMContact() {Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);Log.d(TAG, "cursor count=" + cursor.getCount());while (cursor.moveToNext()) {String[] columnNames = cursor.getColumnNames();for (int i = 0; i < columnNames.length; i++) {String name = cursor.getString(0);String number = cursor.getString(1);String emails = cursor.getString(2);String id = cursor.getString(3);Log.d(TAG, "simcardinfo=" + "name=" + name + "  number=" + number + "  emails=" + emails + "  id=" + id);}}}

但是在原生的联系人app里面查看,发现联系人还在,根本没有被删除,达不到我的要求。联系人的app是通过"content://com.android.contacts/contacts"来访问所有联系人的。没办法,只能自己研究Contacts的源码了,删除的核心代码在ContactDeletionInteraction.java

protected void doDeleteContact(final Uri contactUri) {/** M: Add for SIM Contact @{ */if (!isAdded()) {Log.w(FRAGMENT_TAG, "This Fragment is not add to the Activity.");return;}if (!ContactDeletionInteractionUtils.doDeleteSimContact(mContext,contactUri, mSimUri, mSimIndex, mSubId, this)) {/** @} */mContext.startService(ContactSaveService.createDeleteContactIntent(mContext,contactUri));if (isAdded() && mFinishActivityWhenDone) {Log.d(FRAGMENT_TAG, "doDeleteContact -finished");getActivity().setResult(RESULT_CODE_DELETED);getActivity().finish();}}}

看一下doDeleteSimContact这个方法

    public static boolean doDeleteSimContact(Context context, Uri contactUri, Uri simUri,int simIndex, int subId, Fragment fragment) {Log.i(TAG, "[doDeleteSimContact]simUri: " + simUri + ",simIndex = " + simIndex+ ",subId = " + subId+" fragment isAdded "+fragment.isAdded());if (simUri != null && fragment.isAdded()) {Intent intent = new Intent(context, SIMProcessorService.class);intent.setData(simUri);intent.putExtra(SIMDeleteProcessor.SIM_INDEX, simIndex);intent.putExtra(SIMServiceUtils.SERVICE_SUBSCRIPTION_KEY, subId);intent.putExtra(SIMServiceUtils.SERVICE_WORK_TYPE, SIMServiceUtils.SERVICE_WORK_DELETE);intent.putExtra(SIMDeleteProcessor.LOCAL_CONTACT_URI, contactUri);context.startService(intent);return true;}return false;}

这个管理删除Sim卡联系人的核心类SIMDeleteProcessor.java,代码不多,我全贴出来

package com.mediatek.contacts.simservice;import com.mediatek.contacts.simservice.SIMProcessorManager.ProcessorCompleteListener;
import com.mediatek.contacts.simcontact.SubInfoUtils;
import com.mediatek.contacts.util.Log;import android.content.Context;
import android.content.Intent;
import android.net.Uri;import com.android.contacts.ContactSaveService;
import com.android.contacts.interactions.ContactDeletionInteraction;
import com.mediatek.contacts.simservice.SIMServiceUtils;public class SIMDeleteProcessor extends SIMProcessorBase {private static final String TAG = "SIMDeleteProcessor";private static Listener mListener = null;private Uri mSimUri = null;private Uri mLocalContactUri = null;private int mSimIndex = -1;private Context mContext;private Intent mIntent;private int mSubId = SubInfoUtils.getInvalidSubId();public final static String SIM_INDEX = "sim_index";public final static String LOCAL_CONTACT_URI = "local_contact_uri";public interface Listener {public void onSIMDeleteFailed();public void onSIMDeleteCompleted();}public static void registerListener(Listener listener) {if (listener instanceof ContactDeletionInteraction) {Log.i(TAG, "[registerListener]listener added to SIMDeleteProcessor:" + listener);mListener = listener;}}public static void unregisterListener(Listener listener) {Log.i(TAG, "[unregisterListener]removed from SIMDeleteProcessor: " + listener);mListener = null;}public SIMDeleteProcessor(Context context, int subId, Intent intent,ProcessorCompleteListener listener) {super(intent, listener);Log.i(TAG, "[SIMDeleteProcessor]new...");mContext = context;mSubId = subId;mIntent = intent;}@Overridepublic int getType() {return SIMServiceUtils.SERVICE_WORK_DELETE;}@Overridepublic void doWork() {if (isCancelled()) {Log.w(TAG, "[dowork]cancel remove work. Thread id = "+ Thread.currentThread().getId());return;}mSimUri = mIntent.getData();mSimIndex = mIntent.getIntExtra(SIM_INDEX, -1);mLocalContactUri = mIntent.getParcelableExtra(LOCAL_CONTACT_URI);if (mContext.getContentResolver().delete(mSimUri, "index = " + mSimIndex, null) <= 0) {Log.i(TAG, "[doWork] Delete SIM contact failed");if (mListener != null) {mListener.onSIMDeleteFailed();}} else {Log.i(TAG, "[doWork] Delete SIM contact successfully");mContext.startService(ContactSaveService.createDeleteContactIntent(mContext,mLocalContactUri));if (mListener != null) {mListener.onSIMDeleteCompleted();}}}
}

这里最关键的

mContext.getContentResolver().delete(mSimUri, "index = " + mSimIndex, null)

这个mSimUri是通过根据sim卡的来的,

mSimUri = SubInfoUtils.getIccProviderUri(mSubId);

我插一张卡,打印出来的是"content://icc/pbr/subId/1",mSubId这个值是会变的,插入不同的sim卡,这个值会变,看一下SubInfoUtils.java这个类

public static final String ICC_PROVIDER_SDN_URI = "content://icc/sdn/subId";
public static final String ICC_PROVIDER_ADN_URI = "content://icc/adn/subId";
public static final String ICC_PROVIDER_PBR_URI = "content://icc/pbr/subId";public static Uri getIccProviderUri(int subId) {if (!checkSubscriber(subId)) {return null;}if (SimCardUtils.isUsimOrCsimType(subId)) {return ContentUris.withAppendedId(Uri.parse(ICC_PROVIDER_PBR_URI), subId);} else {return ContentUris.withAppendedId(Uri.parse(ICC_PROVIDER_ADN_URI), subId);}}

根据卡的类型来选用不同的Uri。

但是仅仅删除这个,不能够达到目的,这里还有一处关键代码

mContext.startService(ContactSaveService.createDeleteContactIntent(mContext,mLocalContactUri));

这个是在ContactSaveService.java里面实现的

public static Intent createDeleteContactIntent(Context context, Uri contactUri) {Intent serviceIntent = new Intent(context, ContactSaveService.class);serviceIntent.setAction(ContactSaveService.ACTION_DELETE_CONTACT);serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_URI, contactUri);return serviceIntent;
}
@Override
protected void onHandleIntent(Intent intent) {...//处理删除的intent} else if (ACTION_DELETE_CONTACT.equals(action)) {deleteContact(intent);}...
}private void deleteContact(Intent intent) {Uri contactUri = intent.getParcelableExtra(EXTRA_CONTACT_URI);if (contactUri == null) {Log.e(TAG, "Invalid arguments for deleteContact request");return;}getContentResolver().delete(contactUri, null, null);
}

很明显,这句代码是关键

getContentResolver().delete(contactUri, null, null);

研究完了Contacts,接下来就是自己怎么写的问题了,

public synchronized boolean deleteContactsinfo(String deleteid){try {ContentResolver mContentResolver = context.getContentResolver();Cursor cursor = mContentResolver.query(ContactsContract.Contacts.CONTENT_URI,null,"_id =?", new String[]{deleteid}, null);if(cursor.moveToFirst()) {int index_in_sim = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.INDEX_IN_SIM));Log.d(TAG, "deleteContactsinfo:  "+" index_in_sim "+index_in_sim);//通过index_in_sim 来判断是sim卡中的联系人还是设备中的联系人if(index_in_sim != -1){String lookup_key = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));int indicate_phone_or_sim_contact =  cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.INDICATE_PHONE_SIM));Uri simUri = Uri.parse("content://icc/pbr/subId/"+indicate_phone_or_sim_contact);int delete1 = mContentResolver.delete(simUri, "index = " + index_in_sim, null);int delete5 = mContentResolver.delete(Uri.parse("content://com.android.contacts/contacts/lookup/"+lookup_key+"/"+deleteid),  null,null);Log.d(TAG," delete1 "+delete1+" delete5 "+delete5);}else{mContentResolver.delete(Uri.parse("content://com.android.contacts/raw_contacts"),  "_id =?", new String[]{deleteid});mContentResolver.delete(Uri.parse("content://com.android.contacts/data"), ContactsContract.RawContacts.CONTACT_ID + " =?", new String[]{deleteid});       }cursor.close();}}catch(Exception e){e.printStackTrace();return false;}return true;}

这里是通过index_in_sim来判断是sim卡还是phone中的联系人,如果是phone中的联系人,这个值为-1,同时删除phone中联系人的方法为

mContentResolver.delete(Uri.parse("content://com.android.contacts/raw_contacts"), "_id =?", new String[]{"" + deleteid});mContentResolver.delete(Uri.parse("content://com.android.contacts/data"),ContactsContract.RawContacts.CONTACT_ID + " =?", new String[]{deleteid + ""});

查询所有联系人的方法

  public List<ContactsInfo> getSIMContacts(int curpage) {ContentResolver resolver = context.getContentResolver();Cursor phoneCursor = null;try {Uri uri = Uri.parse("content://com.android.contacts/contacts"); //限定每次查询6条数据String limitye = "limit " + (curpage - 1) * PAGESIZE + "," + PAGESIZE;System.out.println("....... limitye " + limitye);phoneCursor = resolver.query(uri,new String[]{"_id"},null, null, ContactsContract.Contacts._ID + " ASC " + limitye);//ContactsContract.Contacts._ID + " ASC "+limityeint classid = 0;if (phoneCursor != null) {while (phoneCursor.moveToNext()) {messageInfo = new ContactsInfo();int contactsId = phoneCursor.getInt(0);uri = Uri.parse("content://com.android.contacts/contacts/" + contactsId + "/data"); //某个联系人下面的所有数据Cursor dataCursor = resolver.query(uri, new String[]{"mimetype", "data1", "data2"}, null, null, null);String name = "", phone = "";while (dataCursor.moveToNext()) {String data = dataCursor.getString(dataCursor.getColumnIndex("data1"));String type = dataCursor.getString(dataCursor.getColumnIndex("mimetype"));if ("vnd.android.cursor.item/name".equals(type)) {    // 如果他的mimetype类型是namename = data;} else if ("vnd.android.cursor.item/phone_v2".equals(type)) { // 如果他的mimetype类型是phonephone = data;}}if(!name.equals("") && !phone.equals("")){messageInfo.setSmsName(name);messageInfo.setPhoneNumber(phone);messageInfo.setClassid(classid);messageInfo.setContactid(contactsId);System.out.println("dataCursor  getSmsName =" + messageInfo.getSmsName() + "  dataCursor  data1 =" + messageInfo.getPhoneNumber() + " contactsId " + messageInfo.getContactid());classid++;list.add(messageInfo);}}}} catch (SQLiteException e) {e.printStackTrace();} finally {if (phoneCursor != null)phoneCursor.close();}return list;
}

插入sim卡中的联系人

  public void simInsert(String name,String number) {Uri uri = Uri.parse("content://icc/adn");ContentValues values = new ContentValues();values.put("tag", name);values.put("number", number);Uri newSimContactUri =getContentResolver().insert(uri, values);Log.d(TAG,">>>>>>" + "new sim contact uri, " + newSimContactUri.toString());}

插入设备联系人

   public void AddContactsinfo(String phoneNumber,String message){/* 往 raw_contacts 中添加数据,并获取添加的id号*/Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");ContentResolver resolver =getContentResolver();ContentValues values = new ContentValues();long contactId = ContentUris.parseId(resolver.insert(uri, values));/* 往 data 中添加数据(要根据前面获取的id号) */// 添加姓名uri = Uri.parse("content://com.android.contacts/data");values.put("raw_contact_id", contactId);values.put("mimetype", "vnd.android.cursor.item/name");values.put("data2", message);resolver.insert(uri, values);// 添加电话values.clear();values.put("raw_contact_id", contactId);values.put("mimetype", "vnd.android.cursor.item/phone_v2");values.put("data2", "2");values.put("data1", phoneNumber);resolver.insert(uri, values);}

android 怎样删除sim卡中的联系人相关推荐

  1. 获取手机通讯录 (含SIM卡中的联系人)

    sim卡UIR content://icc/fdn content://sim/fdn MOTO XT800比较异常,通讯录在: content://contacts/phones 1.使用andro ...

  2. android+已移除sim卡,如何从Android SIM卡中删除/删除/擦除联系人

    作者:Selena Komez11月更新了20,2017 "我的兄弟是一个空白的经理,他在他的Android SIM卡上保存了数千个客户名称和电话号码.但是,现在为了保护客户的隐私,他需要将 ...

  3. android 监听sim卡状态,android中判断sim卡状态和读取联系人资料的方法

    在写程序中,有时候可能需要获取sim卡中的一些联系人资料.在获取sim卡联系人前,我们一般会先判断sim卡状态,找到sim卡后再获取它的资料,如下代码我们可以读取sim卡中的联系人的一些信息. imp ...

  4. android telephonymanager 电话状态,TelephonyManager类:Android手机及Sim卡状态的获取

    TelephonyManager这个类很有用,可以得到很多关于手机和Sim卡的信息. 直接上注释后的代码,请享用 package net.sunniwell.app; import android.a ...

  5. 读取SIM卡中联系人流程

    本文主要讲USIM卡插入手机后读取卡中contacts信息的流程. 整体流程总结 每次插拔SIM卡都会将联系人数据库中关于SIM卡的联系人删除 SimStateReceiver 通过接收 RIL 上报 ...

  6. android如何读取sim卡联系人信息,Android上如何获取手机和sim卡的联系人信息

    需求很明确,获取手机和sim的联系人信息.接口设计也很简介 public interface OnFetchContactsListener { void onFetcherContactsCompl ...

  7. 原生Android设置sim卡锁定,如何为更安全的Android手机设置SIM卡锁 | MOS86

    有一些事情你可以做,以确保您的Android手机保持尽可能安全,如果它从你的手中流失 - 一个好的锁屏密码是一个坚实的开始.您可能没有意识到的是,有一种方法可以通过启用SIM卡来进一步增强安全性. 相 ...

  8. SIM卡中UCS2编码的三种格式(80,81,82)分析

    为什么手机短信长度限制70个中文.160个英文??? (2012-04-15 00:15:26) 转载▼ 标签: 杂谈 分类:Android 手机短信的长度是由编码决定的,根据国际标准,每条短信最多发 ...

  9. 关于android双卡手机sim卡信息采集适配的心得

    关于android双卡手机sim卡信息采集适配的心得 这几天通过对各个厂商的双卡信息适配的研究,总结了几点规律,写这篇心得主要是为了能够抛砖引玉,和大家一起分享,一起讨论,源码已上传. 一.andro ...

最新文章

  1. Windows下的鱿鱼(Squid)
  2. python监控mysql数据改变_python3小脚本-监控服务器性能并插入mysql数据库
  3. SHELL脚本--简介
  4. 如何用DOS命令批量删除文件?(_desktop.ini 或 thumbs.db)
  5. java监听机制_详解java的事件监听机制和观察者设计模式
  6. Storm精华问答 | 如何处理常见故障?
  7. key-value 多线程server的Linux C++实现
  8. C 库函数 - pow()
  9. KNX转485模块的开发
  10. 浏览器兼容性问题解决方案 · 总结
  11. 安装ubuntu教程
  12. RabbitMQ由浅入深入门全总结(一)
  13. MOS 的TJ TA TC和功耗之类的计算
  14. DHCP服务器搭建操作步骤
  15. 线性代数学习笔记11-2:总复习Part2(相似对角化、对称矩阵、奇异值分解SVD)
  16. 手机扫描计数器有哪些?实用扫描计数软件分享给你
  17. 更新Android版GPS定位源代码
  18. docker内运行的grafana重置登录密码
  19. python语法报错_Python语法总结
  20. Fragment中OnCreate与OnCreateView区别

热门文章

  1. win10 mysql 卸载不干净,安装提示,已经存在
  2. 今日头条2018.8.12笔试题总结
  3. NGNIX在CENTEROS 下部署
  4. 函数最值题目及答案_有关函数的极值与导数的测试题及答案
  5. 快速了解 Robot Operating System(ROS) 机器人操作系统
  6. 零入门kubernetes网络实战-20->golang编程syscall操作tun设备介绍
  7. mybatis源码-plugin源码
  8. 微软开源在线代码编辑器,编辑器天花板之Monaco Editor
  9. 英格兰的政治+德意志的工业科技+犹太的金融+北美的丰富资源=世界NO.1强国
  10. 华为的操作系统即将发布?