Analysis of Android mobile phone guard reading contact person
- 2021-07-06 11:43:26
- OfStack
Recommended reading:
Analysis of Android Mobile Phone Guardian sim Card Binding
In-depth analysis of md5 encryption when Android mobile phone guard saves password
Explanation of Android Mobile Guardian Setup Wizard Page
Analysis of Android Mobile Phone Guardian Turning off Automatic Update
Analysis on the Attributes of Android Mobile Guardian Custom Control
Gets the ContentResolver content parser object, using the getContentResolver () method
Call the query () method of the ContentResolver object to get the data in the raw_contacts table and get the Cursor object
Parameters: Uri object, field String array
Get the Uri object, and use the Uri. parse ("content://com. android. contacts/raw_contacts") method,
while loops the Cursor object, provided that the moveToNext () method of the Cursor object is true
Call the getString () method of the Cursor object with the index as the parameter
Judge that it is not null, and query another table
Call the query () method of the ContentResolver object to get the data in the data table and get the Cursor object
Parameters: Uri object, field String [] array (data1, mimetype), condition String, condition value String [] array (contact_id)
The Uri object is Uri. parse ("content://com. android. contacts/data")
Cycle and the above 1 sample
The type corresponding to the name is vnd. android. cursor. item/name
Telephone correspondence type is vnd. android. cursor. item/phone_v2
Permission required, android. permisssion.READ_CONTACTS
Call the setAdapter () method of the ListView object to assign data to the view with the Adapter object as the argument
Get the Adapter object through new SimpleAdapter ()
Parameters: Context, Data Collection, 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){
ContentResolver 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 list
Cursor cursor=resolver.query(uri, new String[]{"contact_id"}, null, null, null);
while(cursor.moveToNext()){
String id=cursor.getString(cursor.getColumnIndex("contact_id"));
if(id!=null){
Map<String,String> contact=new HashMap<String,String>();
// Lookup 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;
}
}
The above content is the introduction of android mobile phone guard reading contact person, I hope it will be helpful to everyone!