[Android] mobile guard reads contacts, android guard

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.