Android gets phone contact instance code detail

  • 2020-12-07 04:24:51
  • OfStack

My style, blah blah blah, directly to everyone posted the code.

The specific code is as follows:


package com.org.demo.demo;
import com.org.wangfeng.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class GetPhoneActivity extends Activity {
 private EditText editText;
 private Button button;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.getphoneactivity);
  editText = (EditText) findViewById(R.id.et_getphone);
  button = (Button) findViewById(R.id.bb_getphone);
  button.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    Intent intent = new Intent(GetPhoneActivity.this,
      ContactActivity.class);
    startActivityForResult(intent, 1);
   }
  });
 }
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  // System.out.println("resultCode:" + resultCode);
  // System.out.println("requestCode:" + requestCode);
  Log.d("jiejie", " Is called the ");
  if (resultCode == Activity.RESULT_OK) {
   String phone = data.getStringExtra("phone");
   Log.d("jiejie", "_______________"+phone);
   phone = phone.replaceAll("-", "").replaceAll(" ", "");//  replace - And Spaces 

   editText.setText(phone);//  Set the phone number to the input field 
  }
  super.onActivityResult(requestCode, resultCode, data);
 }
}
package com.org.demo.demo;
import java.util.ArrayList;
import java.util.HashMap;
import com.org.wangfeng.R;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
public class ContactActivity extends Activity {
 private ListView lvList;
 private ArrayList<HashMap<String, String>> readContact;
 private TextView back;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_contact);
  back =(TextView)findViewById(R.id.back);
  back.setOnClickListener(new View.OnClickListener() {   
   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    finish();
   }
  });
  lvList = (ListView) findViewById(R.id.lv_list);
  readContact = readContact();
  // System.out.println(readContact);
  lvList.setAdapter(new SimpleAdapter(this, readContact,
    R.layout.contact_list_item, new String[] { "name", "phone" },
    new int[] { R.id.tv_name, R.id.tv_phone }));
  lvList.setOnItemClickListener(new OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> parent, View view,
     int position, long id) {
    String phone = readContact.get(position).get("phone");//  Read the current item Telephone number of 
    Intent intent = new Intent();
    intent.putExtra("phone", phone);
    setResult(Activity.RESULT_OK, intent);//  Put the data in intent Go back to 1 A page 

    finish();
   }
  });
 }
 private ArrayList<HashMap<String, String>> readContact() {
  //  First of all, , from raw_contacts To read a contact in id("contact_id")
  //  The second ,  According to the contact_id from data The corresponding phone number and contact name are retrieved from the table 
  //  then , According to the mimetype To distinguish which one is the contact , Which is the phone number 
  Uri rawContactsUri = Uri
    .parse("content://com.android.contacts/raw_contacts");
  Uri dataUri = Uri.parse("content://com.android.contacts/data");
  ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
  //  from raw_contacts To read a contact in id("contact_id")
  Cursor rawContactsCursor = getContentResolver().query(rawContactsUri,
    new String[] { "contact_id" }, null, null, null);
  if (rawContactsCursor != null) {
   while (rawContactsCursor.moveToNext()) {
    String contactId = rawContactsCursor.getString(0);
    // System.out.println(contactId);
    //  According to the contact_id from data The corresponding phone number and contact name are retrieved from the table ,  You're actually querying the view view_data
    Cursor dataCursor = getContentResolver().query(dataUri,
      new String[] { "data1", "mimetype" }, "contact_id=?",
      new String[] { contactId }, null);
    if (dataCursor != null) {
     HashMap<String, String> map = new HashMap<String, String>();
     while (dataCursor.moveToNext()) {
      String data1 = dataCursor.getString(0);
      String mimetype = dataCursor.getString(1);
      // System.out.println(contactId + ";" + data1 + ";"
      // + mimetype);
      if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) {
       map.put("phone", data1);
      } else if ("vnd.android.cursor.item/name"
        .equals(mimetype)) {
       map.put("name", data1);
      }
     }
     list.add(map);
     dataCursor.close();
    }
   }
   rawContactsCursor.close();
  }
  return list;
 }
}

This is the end of the code, the code is relatively simple, with comments, hope to learn android to get mobile contacts related knowledge to help you.


Related articles: