package com.example.sample_5_10; import android.app.Activity; import android.app.Instrumentation.ActivityResult; import android.content.CursorLoader; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { EditText et1; EditText et2; Button bt; private static final int REQUESTCODE = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et1 = (EditText) findViewById(R.id.editText1); et2 = (EditText) findViewById(R.id.editText2); bt = (Button) findViewById(R.id.button1); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub startActivityForResult( new Intent( Intent.ACTION_PICK, android.provider.ContactsContract.Contacts.CONTENT_URI), REQUESTCODE); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub switch (requestCode) { case REQUESTCODE: final Uri uriret = data.getData(); if (uriret != null) { try { CursorLoader loader = new CursorLoader(MainActivity.this, uriret, null, null, null, null); Cursor c = loader.loadInBackground(); c.moveToFirst(); String strname = c .getString(c .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); et1.setText(strname); int contact_id = c.getInt(c .getColumnIndex(ContactsContract.Contacts._ID)); Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contact_id, null, null); if (phones.getCount() > 0) { phones.moveToFirst(); String phonename = phones .getString(phones .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)); et2.setText(phonename); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } break; default: break; } super.onActivityResult(requestCode, resultCode, data); } }
联系人电话号码默认选首个,需要全部显示可以用for循环phones游标即可。
调用contact某个联系人资料后返回数据到原来的Activity,布布扣,bubuko.com
时间: 2024-10-09 12:30:27