如何删除机器人从SIM卡联系人 [英] How to delete contacts from sim in android

查看:264
本文介绍了如何删除机器人从SIM卡联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没下code删除从SIM卡中选定的联系人。但它不是删除,也没有抛出任何错误。

 保护无效DeleteContacts(ArrayList的<串GT; IDS){        INT FLG = 0;        的String [] = strids新的String [ids.size()];
        strids = ids.toArray(strids);        的for(int i = 0; I< strids.length;我++){            光标SIMS = getActivity()。getContentResolver()查询(
                    Uri.parse(内容:// ICC / ADN),空,
                    _id =?,新的String [] {strids [I]},NULL);            sims.moveToFirst();
            如果(sims.getCount()大于0){
                字符串phoneNumber的= sims.getString(sims.getColumnIndex(数字));
                布尔VAL = deleteContact(phoneNumber的);
                如果(!VAL)
                    FLG = 1;
            }            如果(FLG == 0)
                Toast.makeText(getActivity(),删除联系人,Toast.LENGTH_SHORT).show();
            sims.close();
        }
    }    公共布尔deleteContact(字符串电话){
        光标CUR = getActivity()getContentResolver()查询。(Uri.parse(内容:// ICC / ADN),空?数=,新的String [] {}手机,NULL);
        尝试{
            如果(cur.moveToFirst()){
                做{
                    串lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                    URI URI = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI,lookupKey);
                    getActivity()getContentResolver()删除(URI,NULL,NULL);
                    返回true;
                }而(cur.moveToNext());
            }
        }赶上(例外五){
            的System.out.println(e.getStackTrace());
        }
        返回false;
    }


解决方案

根据 href=\"http://stackoverflow.com/a/27844393/2003986\">我的答案,这里是解决方案:

 乌里simUri = Uri.parse(内容:// ICC / ADN /);
ContentResolver的mContentResolver = this.getContentResolver();
光标C = mContentResolver.query(simUri,NULL,NULL,NULL,NULL);
如果(c.moveToFirst())
{
    做
    {
        如果(/ *您在这里的条件* /)
        {
            mContentResolver.delete(
                simUri,
                标签='+ c.getString(c.getColumnIndex(名称))+
                '和+
                数='+ c.getString(c.getColumnIndex(数字))+'
                , 空值);
            打破;
        }
    }
    而(c.moveToNext());
}

当然,不要忘记这些权限:

 <使用许可权的android:NAME =android.permission.READ_CONTACTS/>
<使用许可权的android:NAME =android.permission.WRITE_CONTACTS/>

I did following code for deleting selected contacts from sim card. but its not deleting and also not throwing any error.

protected void DeleteContacts(ArrayList<String> ids){

        int flg = 0;

        String[] strids = new String[ids.size()];
        strids = ids.toArray(strids);

        for (int i = 0; i < strids.length; i++) {

            Cursor sims = getActivity().getContentResolver().query(
                    Uri.parse("content://icc/adn"), null,
                    "_id=?", new String[]{strids[i]}, null);

            sims.moveToFirst();
            if (sims.getCount()>0) {
                String phoneNumber = sims.getString(sims.getColumnIndex("number"));
                boolean val = deleteContact(phoneNumber);
                if (!val)
                    flg=1;
            }

            if (flg == 0)
                Toast.makeText(getActivity(), "Contact Deleted", Toast.LENGTH_SHORT).show();
            sims.close();
        }
    }

    public boolean deleteContact(String phone) {
        Cursor cur = getActivity().getContentResolver().query(Uri.parse("content://icc/adn"), null, "number=?", new String[] { phone }, null);
        try {
            if (cur.moveToFirst()) {
                do {
                    String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                    Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
                    getActivity().getContentResolver().delete(uri, null, null);
                    return true;
                } while (cur.moveToNext());
            }
        } catch (Exception e) {
            System.out.println(e.getStackTrace());
        }
        return false;
    }

解决方案

Based on my answer here, here is the solution :

Uri simUri = Uri.parse("content://icc/adn/");
ContentResolver mContentResolver = this.getContentResolver();
Cursor c = mContentResolver.query(simUri, null, null, null, null);
if (c.moveToFirst())
{
    do
    {
        if (/* your condition here */)
        {
            mContentResolver.delete(
                simUri,
                "tag='" + c.getString(c.getColumnIndex("name")) +
                "' AND " +
                "number='" + c.getString(c.getColumnIndex("number")) + "'"
                , null);
            break;
        }                       
    }
    while (c.moveToNext());
}

Off course, don't forget these permissions :

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />

这篇关于如何删除机器人从SIM卡联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆