当前位置: 代码迷 >> Android >> android开发 查寻通讯录取得联系人信息apk
  详细解决方案

android开发 查寻通讯录取得联系人信息apk

热度:91   发布时间:2016-05-01 14:19:53.0
android开发 查找通讯录取得联系人信息apk
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class PhoneBook extends Activity {

/*声明四个UI变量与一个常数作为Activity接收回传值用*/
  private TextView mTextView01;
  private Button mButton01;
  private EditText mEditText01;
  private EditText mEditText02;
  private static final int PICK_CONTACT_SUBACTIVITY = 2;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    /*透过findViewById建构子来建构一个TextView,两个EditText,一个Button对象**/
    mTextView01 = (TextView)findViewById(R.id.myTextView1);
    mEditText01 = (EditText)findViewById(R.id.myEditText01);
    mEditText02 = (EditText)findViewById(R.id.myEditText02);
    mButton01 = (Button)findViewById(R.id.myButton1);
    /*设定onClickListener 让使用者点选Button时搜寻联系人*/
    mButton01.setOnClickListener(new Button.OnClickListener()
    {
     
//     @Override
    public void onClick(View v)
    {
      // TODO Auto-generated method stub
      /*建构Uri来取得联络人的资源位置*/
      // Uri uri = Uri.parse("content://contacts/people/");
      /*透过Intent来取得联络人数据并回传所选的值*/
      //Intent intent = new Intent(Intent.ACTION_PICK, uri);
      /*开启新的Activity并期望该Activity回传值*/
      //startActivityForResult(intent, PICK_CONTACT_SUBACTIVITY);
      startActivityForResult
      (
          new Intent(Intent.ACTION_PICK,
              android.provider.ContactsContract.Contacts.CONTENT_URI),
              PICK_CONTACT_SUBACTIVITY);
      }
    });
    }
  @Override
  protected void onActivityResult (int requestCode, int resultCode, Intent data)
  {
    // TODO Auto-generated method stub
    try
    {
      switch (requestCode)
      {
        case PICK_CONTACT_SUBACTIVITY:
          final Uri uriRet = data.getData();
          if(uriRet != null)
          {
            try
            {
              /* 必须要有android.permission.READ_CONTACTS权限 */
              Cursor c = managedQuery(uriRet, null, null, null, null);
              /*将Cursor移到资料最前端*/
              c.moveToFirst();
              /*取得联络人的姓名*/
              String strName = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
              /*将姓名写入EditText01中*/
              mEditText01.setText(strName);
              /*取得联络人的电话*/
              int contactId = c.getInt(c.getColumnIndex(ContactsContract.Contacts._ID));
              Cursor phones = getContentResolver().query ( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null );
              StringBuffer sb = new StringBuffer();
              int typePhone, resType;
              String numPhone;
              if (phones.getCount() > 0)
              {
                phones.moveToFirst();
                /* 2.0可以允许User设定多组电话号码,但本范例只捞一组电话号码作示范 */
                typePhone = phones.getInt ( phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE) );
                numPhone = phones.getString ( phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER) );
                resType = ContactsContract.CommonDataKinds.Phone.getTypeLabelResource(typePhone);
                sb.append(getString(resType) +": "+ numPhone +"\n");
                /*将电话写入EditText02中*/
                mEditText02.setText(numPhone);
                }
              else
              {
                sb.append("no Phone number found");
                }
              /*Toast是否读取到完整的电话种类与电话号码*/
              Toast.makeText(this, sb.toString(), Toast.LENGTH_SHORT).show();
              }
            catch(Exception e)
            {
              /*将错误信息在TextView中显示*/
              mTextView01.setText(e.toString());
              e.printStackTrace();
              }
            }
          break;
          default: break;
          }
      }
    catch(Exception e)
    {
      e.printStackTrace();
      }
    super.onActivityResult(requestCode, resultCode, data);
    }
}
-------------------------
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="app_name">PhoneBook</string>
  <string name="str_button1">搜索</string>
  <string name="str_title">我的联系人</string>
  <string name="str_name">姓名</string>
  <string name="str_telephone">电话号码</string>
</resources>
-----------------------------
main.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
  android:id="@+id/widget32"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android"
>
  <TextView
    android:id="@+id/myTextView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/str_title"
    android:layout_x="0px"
    android:layout_y="0px"
  >
  </TextView>
  <EditText
    android:id="@+id/myEditText01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/str_name"
    android:textSize="18sp"
    android:layout_x="0px"
    android:layout_y="22px"
  >
  </EditText>
  <EditText
    android:id="@+id/myEditText02"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/str_telephone"
    android:textSize="18sp"
    android:layout_x="0px"
    android:layout_y="82px"
  >
  </EditText>
  <Button
    android:id="@+id/myButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/str_button1"
    android:layout_x="10px"
    android:layout_y="142px"
  >
  </Button>
</AbsoluteLayout>

------------------------------------
AndroidManifest.xml
注意在</application>后要加上
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
<!--取得读取通讯录的权限 -->
1 楼 samyou 2011-11-01  
好东西用着了,感谢分享
  相关解决方案