通过onActivityResult()先跳转到联系人界面,然后把传回来的手机号显示到应用的EditText上

<pre name="code" class="plain"><pre name="code" class="plain">public class MainActivity extends Activity {
	/**输入框*/
    private EditText et;
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et = (EditText) findViewById(R.id.et);
    }
	//点击跳转到联系人界面
    public void click(View v){
    	Intent intent = new Intent();
    	intent.setAction("android.intent.action.PICK");
    	intent.setData(Uri.parse("content://com.android.contacts/contacts"));
    	startActivityForResult(intent, 5);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	super.onActivityResult(requestCode, resultCode, data);
    	if(requestCode==5){
    		Uri uri = data.getData();
    		Cursor cursor = getContentResolver().query(uri, new String[]{"_id"}, null, null, null);
    		cursor.moveToNext();
    		int id = cursor.getInt(0);//联系人的Id
    		
    		//再通过ID查号码
    		Cursor cursor2 = getContentResolver().query(Phone.CONTENT_URI,  //获取所有联系人的URL
    				new String[]{"data1"}, 	//表示要查号码
    				"contact_id=?", 	//通过联系人的ID查
    				new String[]{id+""}, null);
    		cursor2.moveToNext();
    		String number = cursor2.getString(0);//获取联系人号码
    		et.setText(number);
    	}
    }
}




在小米手机上测试有问题,联系人显示不出来,不知道为什么?

这是布局




   

posted @ 2015-11-05 20:17  四念处  阅读(284)  评论(0编辑  收藏  举报