Android mobile guard-obtains and displays contact information and ECHO, android echo

Source: Internet
Author: User

Android mobile guard-obtains and displays contact information and ECHO, android echo

The previous article has implemented related la S. This article then implements relevant functions.

For more information, see http://www.cnblogs.com/wuyudong/p/5951794.html.

Read system contacts

After you click "select contact", the contact list is displayed. The following steps are taken to read the system contact:

The system contact provides a content provider that matches the Url address through the content parser.

1. content parser

2. Url address: view the system contact database and content provider source code

First look at the list file of the api documentation, then look at the java class (the contact database has multiple tables)

Contents: // com. android. contacts/table name

3. Table Structure of the core table in the system contact database

Raw_contacts contact table: contact_id unique id of a contact

Data User information table: raw_contact_id is used as the foreign key and is associated with the contact_id in raw_contacts.

Obtains the data1 field, including the phone number and contact name.

The mimetype_id field contains the data type corresponding to the forward data1.

Mimetypes type table: obtains the id of the mimetype_id and mimetypes in the data table for association query and obtains the information type pointed.
Phone number: vnd. android. cursor. item/phone_v2
User name: vnd. android. cursor. item/name

4. Table Access Method

Content: // com. android. contacts/raw_contacts
Content: // com. android. contacts/data

The following code is used to implement

Private ListView lv_contact; private List <HashMap <String, String> contactList = new ArrayList <HashMap <String, String> (); private MyAdapter mAdapter; private Handler mHandler = new Handler () {@ Override public void handleMessage (Message msg) {// 8, fill in the data adapter mAdapter = new MyAdapter (); lv_contact.setAdapter (mAdapter) ;};@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (SavedInstanceState); setContentView (R. layout. activity_contact_list); initUI (); initData ();} class MyAdapter extends BaseAdapter {@ Override public int getCount () {return contactList. size () ;}@ Override public HashMap <String, String> getItem (int I) {return contactList. get (I) ;}@ Override public long getItemId (int I) {return I ;}@ Override public View getView (int I, View view, ViewGroup viewGroup) {View v = View. inflate (getApplicationContext (), R. layout. listview_contact_item, null); TextView TV _name = (TextView) v. findViewById (R. id. TV _name); TextView TV _phone = (TextView) v. findViewById (R. id. TV _phone); TV _name.setText (getItem (I ). get ("name"); TV _phone.setText (getItem (I ). get ("phone"); return v ;}/ *** method for obtaining contact data */private void initData () {// because the system contact is read, it may be a time-consuming operation, placed in the Child Thread to process new Thread (){ Public void run () {// 1, get the content parser object ContentResolver contentResolver = getContentResolver (); // 2, perform the process of querying the contact database table (read the contact permission) cursor cursor = contentResolver. query (Uri. parse ("content: // com. android. contacts/raw_contacts "), new String [] {" contact_id "}, null, null); contactList. clear (); // 3, loop cursor until no data is available while (cursor. moveToNext () {String id = cursor. getString (0); // 4. query the views generated by the data table and mimetype table based on the unique id value, Obtain the data and mimetype field Cursor indexCursor = contentResolver. query (Uri. parse ("content: // com. android. contacts/data "), new String [] {" data1 "," mimetype "}," raw_contact_id =? ", New String [] {id}, null); // 5. Obtain the phone number and name of each contact cyclically. The data type is HashMap. <String, string> hashMap = new HashMap <String, String> (); while (indexCursor. moveToNext () {String data = indexCursor. getString (0); String type = indexCursor. getString (1); // 6, differentiate the type to fill the hashMap with data if (type. equals ("vnd. android. cursor. item/phone_v2 ") {// if (! TextUtils. isEmpty (data) {hashMap. put ("phone", data) ;}} else if (type. equals ("vnd. android. cursor. item/name ") {if (! TextUtils. isEmpty (data) {hashMap. put ("name", data) ;}} indexCursor. close (); contactList. add (hashMap);} cursor. close (); // 7. The message mechanism sends an empty message to inform the main thread that the mHandler can use the data set that has been filled by the subthread. sendEmptyMessage (0 );}}. start ();}

The implementation result is as follows:

Contact Information Display

Next, click a contact entry to display it back. For example, if you double-click the first entry, the number is automatically added.

The Code is as follows:

Private void initUI () {lv_contact = (ListView) findViewById (R. id. lv_contact); listener (new AdapterView. OnItemClickListener () {@ Override public void onItemClick (AdapterView <?> AdapterView, View view, int I, long l) {// 1. Obtain the if (mAdapter! = Null) {HashMap <String, String> hashMap = mAdapter. getItem (I); // 2. Obtain the phone number that the current entry points to the set. String phone = hashMap. get ("phone"); // 3. This phone number must be used on the third navigation interface. // 4. When the phone number is returned to the previous navigation interface, intent intent = new Intent (); intent. putExtra ("phone", phone); setResult (0, intent); finish ();}}});}

Add the following code to onActivityResult.

@ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {if (data! = Null) {// 1. When the result is returned to the current interface, the method String phone = data. getStringExtra ("phone"); // 2. filter special characters (dashes into empty strings) by phone = phone. replace ("-",""). replace ("",""). trim (); et_phone_number.setText (phone); // 3. Store the contact to SpUtil in the sp. putString (getApplicationContext (), ConstantValue. CONTACT_PHONE, phone);} super. onActivityResult (requestCode, resultCode, data );}

After entering the number, go to the next page and return again. If the number is missing, use sp storage and read it from it.

Private void initUI () {// display the phone number input box et_phone_number = (EditText) findViewById (R. id. et_phone_number); // obtain the contact's phone number during the echo process String contact_phone = SpUtil. getString (this, ConstantValue. CONTACT_PHONE, ""); et_phone_number.setText (contact_phone); bt_select_number = (Button) findViewById (R. id. bt_select_number); // click bt_select_number.setOnClickListener (new View. onClickListener () {@ Override public void onClick (View view) {Intent intent = new Intent (getApplicationContext (), ContactListActivity. class); startActivityForResult (intent, 0 );}});}

 

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.