这个功能我看了不少的资料,但总是删除不成功,我是在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;
}
@Override
public int getType() {
return SIMServiceUtils.SERVICE_WORK_DELETE;
}
@Override
public 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 "+limitye
int 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类型是name
name = data;
} else if ("vnd.android.cursor.item/phone_v2".equals(type)) { // 如果他的mimetype类型是phone
phone = 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);
}