首页 > 代码库 > 调用contact某个联系人资料后返回数据到原来的Activity
调用contact某个联系人资料后返回数据到原来的Activity
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游标即可。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。