In the writing program, sometimes you may need to get some contact information from the SIM card. Before obtaining the SIM card contact, we usually first determine the SIM card status, find the SIM card and then obtain its data, the following code we can read the SIM card contact information.
Phonetest.java Packagecom.android.test; Importandroid.app.Activity; ImportAndroid.content.Context; Importandroid.content.Intent; ImportAndroid.database.Cursor; ImportAndroid.net.Uri; ImportAndroid.os.Bundle; ImportAndroid.telephony.TelephonyManager; ImportAndroid.widget.TextView; Public classPhonetestextendsActivity {PrivateTextView Mtextview; protectedCursor Mcursor =NULL; PrivateTelephonymanager Mtelephonymanager; PrivateString mstring = ""; /**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); Mtextview=(TextView) Findviewbyid (R.id.text); Mtextview.settextsize (20.3f); Issimexist (); if(getsimstate () = =Telephonymanager.sim_state_ready) {mstring+ = "card exists \ n"; Getsimcontacts ("Content://icc/adn");//generally use this one, if this does not work, you can try the following one. Getsimcontacts ("Content://sim/adn");//This kind of reading can not be read in our mobile phone, so it is better to use the last URI. } mtextview.settext (mstring); } Private voidgetsimcontacts (String str) {Intent Intent=NewIntent (); Intent.setdata (Uri.parse (str)); Uri URI=Intent.getdata (); Mcursor= Getcontentresolver (). Query (URI,NULL,NULL,NULL,NULL); if(Mcursor = =NULL) {mstring+ = "Cannot read data from" + str + "\ n"; return ; } mstring+ = "first column:" + mcursor.getcolumnname (0) + "\ n"; Mstring+ = "second column:" + mcursor.getcolumnname (1) + "\ n"; Mstring+ = "third column:" + mcursor.getcolumnname (2) + "\ n"; Mstring+ = "Fourth column:" + mcursor.getcolumnname (3) + "\ n"; Mstring+ = "Number of columns:" + mcursor.getcolumncount () + "\ n"; Mstring+ = "line number:" + mcursor.getcount () + "\ n"; if(Mcursor! =NULL) { while(Mcursor.movetonext ()) {//get name of contact person intNamefieldcolumnindex = Mcursor.getcolumnindex ("name"); Mstring+ = Mcursor.getstring (namefieldcolumnindex) + ""; //Get Phone number intNumberfieldcolumnindex =mcursor. Getcolumnindex ("Number"); Mstring+ = Mcursor.getstring (numberfieldcolumnindex) + ""; //Get Email intEmailsfieldcolumnindex =mcursor. Getcolumnindex ("Emails"); Mstring+ = Mcursor.getstring (emailsfieldcolumnindex) + ""; //Get ID intIdfieldcolumnindex =mcursor. Getcolumnindex ("_ID"); Mstring+ = Mcursor.getstring (idfieldcolumnindex) + "\ n"; }} mstring+ = Mcursor + "\ n"; Mcursor.close (); } Private intgetsimstate () {returnmtelephonymanager.getsimstate (); } Private voidissimexist () {Mtelephonymanager=(Telephonymanager) Getsystemservice (Context.telephony_service); intSimstate =mtelephonymanager.getsimstate (); Switch(simstate) { CaseTelephonyManager.SIM_STATE_ABSENT:mString= "No card"; //Do something Break; CaseTelephonyManager.SIM_STATE_NETWORK_LOCKED:mString= "Requires Networkpin to unlock"; //Do something Break; CaseTelephonyManager.SIM_STATE_PIN_REQUIRED:mString= "Requires pin to unlock"; //Do something Break; CaseTelephonyManager.SIM_STATE_PUK_REQUIRED:mString= "Requires pun to unlock"; //Do something Break; CaseTelephonyManager.SIM_STATE_READY:mString= "good"; //Do something Break; CaseTelephonyManager.SIM_STATE_UNKNOWN:mString= "Unknown State"; //Do something Break; } mtextview.settext (mstring); }} [code] main.xml<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "Vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" > <scrollview android:layout_width= "fill_parent"Android:layout_height= "Fill_parent" > <linearlayout android:orientation= "Vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" > <textview android:id= "@+id/text"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/hello"/> </LinearLayout> </ScrollView> </LinearLayout>[code] androidmanefist.xml<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.android.test"Android:versioncode= "1"Android:versionname= "1.0" > <application android:icon= "@drawable/icon" android:label= "@string/app_name" > <acti Vity android:name= ". Phonetest "Android:label= "@string/app_name" > <intent-filter> <action android:name= "android.intent.action . MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-f ilter> </activity> </application> <uses-permission android:name= "Android.permissio N.read_contacts "></uses-permission> </manifest>