Android: how to access the SIM contact table using the SDK?
Asked Answered
A

3

7

I am trying to use this query to read the contacts on the SIM.

            cur = managedQuery(Uri.parse("content://icc/adn")
                ,null
                ,null
                ,null
                ,null
                );

The application has READ_CONTACTS and WRITE_CONTACTS permissions. Yet, the query returns an exception.

java.lang.NullPointerException
    at android.os.Parcel.readException(Parcel.java:1224)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:160)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
    at android.content.ContentProviderProxy.bulkQuery(ContentProviderNative.java:369)
    at android.content.ContentProviderProxy.query(ContentProviderNative.java:388)
    at android.content.ContentResolver.query(ContentResolver.java:202)
    at android.app.Activity.managedQuery(Activity.java:1502)
    at com.example.delirious.delirio.onCreate(delirio.java:38)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
    at android.app.ActivityThread.access$2200(ActivityThread.java:126)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4595)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    at dalvik.system.NativeStart.main(Native Method)

What's wrong?

Alidus answered 8/7, 2010 at 21:15 Comment(5)
content://icc/adn is not part of the Android SDK.Penutian
Then, again, what is part of the Android SDK and gives access to the SIM contacts? The code was inspired by groups.google.com/group/android-developers/browse_thread/thread/… : the author uses content://icc/adn and claims he can read contacts (and insert, but not delete them). Also, replacing content://icc/adn with gibberish like content://icc/asdasd causes a different exception, something like "URI unknown": it means that content://icc/adn has some meaning.Alidus
The same works on the Android emulator, not on a real phone. The URIs content://icc/fdn and content://icc/sdn work on both emulator and phoneAlidus
Just have a look at this link #15563290 This code works goodShad
In IccProvider -> ADN (Abbreviated dialing number), FDN (Fixed dialer number), SDN (Service dialing number)Clive
H
8

i have used the following code to get the simcard details..It works fine

Uri simUri = Uri.parse("content://icc/adn");
        Cursor cursorSim    = this.getContentResolver().query(simUri, null, null,null, null);

         while (cursorSim.moveToNext()) {           
             listName.          add(cursorSim.getString(cursorSim.getColumnIndex("name")));
             listContactId.     add(cursorSim.getString(cursorSim.getColumnIndex("_id")));      
             listMobileNo.      add(cursorSim.getString(cursorSim.getColumnIndex("number")));
            }

Here the name, _id, number are column names from the simcard table

Howund answered 15/12, 2010 at 10:54 Comment(2)
are you sure that the content URI will stay the same in future releases?Healthy
What if I want to delete the contact present in SIM?Epitomize
U
2

i got it,

String simUrl = "content://icc/adn";
Intent intent = new Intent();
            Log.d(TAG, "simUrl=" + simUrl);
            intent.setData(Uri.parse(simUrl));
            Uri uri = intent.getData();
            Cursor mCursor = context.getContentResolver().query(uri, null,
                    null, null, null);

and then, for(...){...}, you know

Unscrew answered 9/12, 2010 at 8:24 Comment(2)
Cursor mCursor = context.getContentResolver().query(uri, null, null, null, null); causes the same exception in this case as well...Alidus
OK, it maybe the environment problems, you have to get the sim card state in ("no known" || "ready"). before you run this, please restart your device.Unscrew
C
2

I just implemented a small piece of code that used to display a list of contacts in SIM card. Hope that it can help you

private void displaySIMContacts() {
    try {
        String simPhoneId = null;
        String simPhoneNum = null;
        String simPhoneName = null;

        Uri simUri = Uri.parse("content://icc/adn");
        Cursor simCursor = getContentResolver().query(simUri, null, null, null, null);

        while(simCursor.moveToNext()) {
            simPhoneId = simCursor.getString(simCursor.getColumnIndex("_id"));
            simPhoneNum = simCursor.getString(simCursor.getColumnIndex("name"));
            simPhoneName = simCursor.getString(simCursor.getColumnIndex("number"));
            Log.v("!!!", " id = " + simPhoneId + " - name = " + simPhoneName
                + " - number = " +simPhoneNum);
        }
    }
    catch (Exception e) {
       e.printStackTrace();
    }
}
Chaos answered 8/5, 2013 at 9:27 Comment(1)
What if I want to delete the contact present in SIM?Epitomize

© 2022 - 2024 — McMap. All rights reserved.