Android gets contacts from the system, android gets contacts

Source: Internet
Author: User

Android gets contacts from the system, android gets contacts

This document describes how to obtain contact data of the system in android.

First open the simulator


Click the contact icon.


The system contact database is empty. Open File explorer and find the folder under data/data:


When you export the contacts2.db file to sqlite, an error is returned:


Click "OK" to ignore


The table structure is quite complex. First, we need to figure out the three tables.

* The data table stores the contact data.

* Raw_contacts table: id of the contact to be saved contact_id

* Type of the contact data saved in the mimetypes table

Next, add a contact through the simulator.


Use sqlite to refresh contacts2.db and ignore the error message.

Steps for getting a contact

1. query the raw_contacts table and obtain the contact id.

2. query the data table based on the contact id and obtain the data of this id.

3. differentiate data types based on mimetype

Next, use the code to implement

package com.wuyudong.getcontacts;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
* *
*Get all contacts
*
* @param view
* /
public void click(View view) {
ContentResolver resolver = getContentResolver();
//1. Query the raw u contacts table and take out the contact ID
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
Uri datauri = Uri.parse("content://com.android.contacts/data");
Cursor cursor = resolver.query(uri, new String[] { "contact_id" },
null, null, null);
while (cursor.moveToNext()) {
String id = cursor.getString(0);
System. Out. Println ("contact ID:" + ID);
//2. Query the data table according to the ID of the contact, and take out the data of this ID
//When the system API queries the data table, it is not the real data table, but the view of 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(0);
System.out.println("data1=" + data1);
String mimetype = dataCursor.getString(1);
System.out.println("mimetype=" + mimetype);
}
dataCursor.close();
}
cursor.close();
}
}

Add permission: android. permission. READ_CONTACTS

Print related results after running:

06-18 10:59:41. 556: I/System. out (2127): Contact id: 1
06-18 10:59:41. 636: I/System. out (2127): data1 = 110
06-18 10:59:41. 636: I/System. out (2127): mimetype = vnd. android. cursor. item/phone_v2
06-18 10:59:41. 646: I/System. out (2127): data1 = wuyudong@wuyudong.com
06-18 10:59:41. 646: I/System. out (2127): mimetype = vnd. android. cursor. item/email_v2
06-18 10:59:41. 646: I/System. out (2127): data1 = Wuyudong
06-18 10:59:41. 646: I/System. out (2127): mimetype = vnd. android. cursor. item/name

Through the print information above, you can modify the program

package com.wuyudong.getcontacts;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
* *
*Get all contacts
*
* @param view
* /
public void click(View view) {
ContentResolver resolver = getContentResolver();
//1. Query the raw u contacts table and take out the contact ID
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
Uri datauri = Uri.parse("content://com.android.contacts/data");
Cursor cursor = resolver.query(uri, new String[] { "contact_id" },
null, null, null);
while (cursor.moveToNext()) {
String id = cursor.getString(0);
System. Out. Println ("contact ID:" + ID);
//2. Query the data table according to the ID of the contact, and take out the data of this ID
//When the system API queries the data table, it is not the real data table, but the view of 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(0);
System.out.println("data1=" + data1);
String mimetype = dataCursor.getString(1);
System.out.println("mimetype=" + mimetype);
}
dataCursor.close();
}
cursor.close();
}
}

Print related results after running:

06-18 11:17:57. 312: I/System. out (2380): Contact id: 1
06-18 11:17:57. 452: I/System. out (2380): Mailbox = wuyudong@wuyudong.com
06-18 11:17:57. 452: I/System. out (2380): name = Wuyudong
06-18 11:17:57. 452: I/System. out (2380): QQ = 11111111
06-18 11:17:57. 482: I/System. out (2380): Contact id: 2
06-18 11:17:57. 572: I/System. out (2380): QQ = 32423423422
06-18 11:17:57. 572: I/System. out (2380): Mailbox = wu@wuyudong.com
06-18 11:17:57. 572: I/System. out (2380): name = Zhangsan

Finally, encapsulate these operations into APIs for future use.


Create a ContactInfoParser. java File

Package com. Wuyudong. Getcontacts. Service;

Import the Java. Util. ArrayList;
Import the Java. Util. List;

The import com. Wuyudong. Getcontacts. Domain. ContactInfo;

The import android. Content. ContentResolver;
The import android. The content. The Context;
The import android. Database. Cursor;
The import android.net.Uri;

Public class ContactInfoParser {
/ * *
* get API methods for all contacts in the system
*
* @ param context
* @ return
* /
Public static List<contactinfo> findAll(Context) {</contactinfo>
ContentResolver resolver = context. GetContentResolver ();
// 1. Query the raw_contacts table and pull out the id of the contact
Uri Uri = Uri. Parse (" content: / / com. Android. Contacts/raw_contacts ");
Uri datauri = Uri. Parse (" content: / / com. Android. Contacts/data ");
List<contactinfo> infos = new ArrayList<contactinfo>();</contactinfo></contactinfo>
Cursor Cursor = resolver.query(uri, new String[] {"contact_id"},
Null, null, null);
While (cursor. MoveToNext ()) {
String id = cursor. Get String (0);
If (id! = null) {
System.out.println(" contact id: "+ id);
ContactInfo info = new ContactInfo();
Info. SetId (id);
// 2. According to the contact's id, query the data table and fetch the data of this id
// when the system API queries the data table, it is not the real query data table, but the query view of 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. Get String (0);
String mimetype = dataCursor. Get String (1);
If (" VND. Android. The cursor. The item/name ". The equals (mimetype)) {
System.out.println(" name =" + data1);
Info. Elegantly-named setName (data1);

} else if (" VND. Android. The cursor. The item/email_v2"
The equals (mimetype)) {
System.out.println(" mailbox =" + data1);
Info. SetEmail (data1);
} else if (" VND. Android. The cursor. The item/phone_v2 ". The equals (data1)) {
System.out.println(" phone =" + data1);
Info. SetPhone (data1);
} else if (" VND. Android. The cursor. The item/im ". The equals (mimetype)) {
System. The out. Println (" QQ = "+ data1);
Info. SetQq (data1);
}

}
Infos. Add (info);
System. The out. Println (" -- -- -- -- -- -- -- ");
DataCursor. Close ();
}
}
Cursor. The close ();
Return infos.

}

}

Create ContactInfo. java

package com.wuyudong.getcontacts.domain;

public class ContactInfo {
    
    private String name;
    private String id;
    private String phone;
    private String email;
    private String qq;
    
    @Override
    public String toString() {
        return "ContactInfo [name=" + name + ", id=" + id + ", phone=" + phone
                + ", email=" + email + ", qq=" + qq + "]";
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getQq() {
        return qq;
    }
    public void setQq(String qq) {
        this.qq = qq;
    }
    
    

}

Finally, call the relevant api

Package com. Wuyudong. Getcontacts;

Import the Java. Util. List;

The import com. Wuyudong. Getcontacts. Domain. ContactInfo;
The import com. Wuyudong. Getcontacts. Service. ContactInfoParser;

The import android.net.Uri;
The import android. OS. Bundle;
The import android. App. The Activity;
The import android. Content. ContentResolver;
The import android. Database. Cursor;
The import android. View. The Menu;
The import android. View. The view;

Public class MainActivity extends Activity {

@ Override
Protected void onCreate(Bundle savedInstanceState) {
Super. OnCreate (savedInstanceState);
The setContentView (R.l ayout. Activity_main);
}

/ * *
* get all the contacts
*
* @ param view
* /
Public void click(View View) {
A List < ContactInfo > infos = ContactInfoParser. The.findall (this);
For (ContactInfo info: infos) {
System. The out. Println (info. The toString ());
}

}

} 

 


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.