[Android] mobile guard reads contacts, android guard
Obtains the ContentResolver content parser object through the getContentResolver () method.
Call the query () method of the ContentResolver object to obtain the data in the raw_contacts table and obtain the Cursor object.
Parameter: Uri object, field String Array
Obtain the Uri object by using the Uri. parse ("content: // com. android. contacts/raw_contacts") method,
While loop Cursor object, with the condition that the moveToNext () method of the Cursor object is true
Call the getString () method of the Cursor object. The parameter is an index.
If it is not null, query another table.
Call the query () method of the ContentResolver object to obtain the data in the data table and obtain the Cursor object.
Parameter: Uri object, field String [] array (data1, mimetype), condition String, condition value String [] array (contact_id)
Uri object is Uri. parse ("content: // com. android. contacts/data ")
Loop is the same as above
The name type is vnd. android. cursor. item/name.
The telephone type is vnd. android. cursor. item/phone_v2.
Permission required. android. permisssion. READ_CONTACTS
Call the setAdapter () method of the ListView object to allocate data to the view. The parameter is the Adapter object.
Use new SimpleAdapter () to obtain the Adapter object
Parameters: context, data set, layout resource, field String [] array, control int [] id Array
Package com. qingguow. mobilesafe. utils; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import android. content. contentResolver; import android. content. context; import android. database. cursor; import android.net. uri;/*** read the mobile phone contact * @ author taoshihan **/public class PhoneContactsUtil {public static List <Map <String, String> getContacts (Context context) {Cont EntResolver resolver = context. getContentResolver (); Uri uri = Uri. parse ("content: // com. android. contacts/raw_contacts "); Uri dataUri = Uri. parse ("content: // com. android. contacts/data "); List <Map <String, String> contacts = new ArrayList <Map <String, String> (); // circular contact table Cursor cursor = resolver. query (uri, new String [] {"contact_id"}, null); while (cursor. moveToNext () {String id = cursor. getString (curso R. getColumnIndex ("contact_id"); if (id! = Null) {Map <String, String> contact = new HashMap <String, String> (); // query the data table Cursor dataCursor = resolver. query (dataUri, new String [] {"data1", "mimetype"}, "raw_contact_id =? ", New String [] {id}, null); while (dataCursor. moveToNext () {String data1 = dataCursor. getString (dataCursor. getColumnIndex ("data1"); String mimetype = dataCursor. getString (dataCursor. getColumnIndex ("mimetype"); System. out. println ("data1:" + data1 + ", mimetype:" + mimetype); if (mimetype. equals ("vnd. android. cursor. item/name ") {contact. put ("name", data1);} else if (mimetype. equals ("vnd. android. cursor. item/phone_v2 ") {contact. put ("phone", data1) ;}} contacts. add (contact); dataCursor. close () ;}} cursor. close (); return contacts ;}}