本文共 1019 字,大约阅读时间需要 3 分钟。
一般情况下我们会通过startActivity()来启动一个activity,但是如果我们想获得要启动的activity的结果的时候就需要调用startActivityForResult().并且重写 onActivityResult() 。 static final int PICK_CONTACT_REQUEST = 1; // The request code ... private void pickContact() { Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts")); pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST); } 得到结果 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // Check which request we're responding to if (requestCode == PICK_CONTACT_REQUEST) { // Make sure the request was successful if (resultCode == RESULT_OK) { // The user picked a contact. // The Intent's data Uri identifies which contact was selected. // Do something with the contact here (bigger example below) } } } 注意要判断requestCode == PICK_CONTACT_REQUEST 且resultCode == RESULT_OK。 如上所示:onActivityResult 是用Intent 来返回数据的。这个时候我们就可以通过Intent的getData()来获得数据.转载地址:http://vvcmi.baihongyu.com/