How to delete contacts from sim in android

爷,独闯天下 提交于 2019-12-11 01:41:02

问题


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;
    }

回答1:


You are deleting the contacts from the sqlite database and not the SIM card. For deleting contacts from the SIM card, you need to delete on Uri.parse("content://icc/adn") uri only. But for deletion you need to provide both name and number. Name column has to specified as tag. Check for delete method here https://android.googlesource.com/platform/frameworks/opt/telephony/+/9ebea45a36838f0547a9c30f7cd9c60b03aab3b4/src/java/com/android/internal/telephony/IccProvider.java




回答2:


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" />


来源:https://stackoverflow.com/questions/25257671/how-to-delete-contacts-from-sim-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!