启动另一个活动不一定是单向的。你也可以开始另一个活动和接收一个返回结果。获得结果,调用startActivityForResult()(而不是startActivity())。
例如,您的应用程序就可以开始一个摄像头应用程序和接收捕获的照片。或者,你可能开始应用的人为了让用户选择一个联系人,你会收到联系信息。
当然,响应的活动必须返回一个结果。时,它发送结果作为另一个意图对象。你的活动收到onActivityResult()回调。
注意:您可以使用显式或隐式意图当你叫startActivityForResult()。当开始一个你自己的活动获得的结果,您应该使用一个显式意图,以确保您得到预期的结果。
开始活动
没什么特别的意图对象你开始一个活动时使用的结果,但你需要通过一个额外的整数参数startActivityForResult()方法。
整数参数是一个“请求代码”,表明你的要求。当你收到结果的意图,回调提供了相同的请求代码以便应用程序能正确识别结果并决定如何处理它。
例如,下面是如何开始一个活动,允许用户选择一个联系人:
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); }收到的结果
当用户完成了后续活动和回报,系统调用活动的onActivityResult()方法。该方法包括三个参数:
请求代码传递给startActivityForResult()。
结果代码指定的第二个活动。这是RESULT_OK如果操作成功或者RESULT_CANCELED如果用户退出或操作由于某种原因失败。
一个意图,结果数据。
举例来说,这里的如何处理“选择一个接触”意图的结果:
@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) } } }
在本例中,返回的结果意图Android的联系人或人们应用程序提供了一个内容Uri标识用户选定的联系人。
为了成功地处理结果,你必须理解结果的格式什么意图。这样做是很容易的,当活动返回一个结果是一个你自己的活动。应用程序包含在Android平台提供自己的api,你可以指望为特定的结果数据。例如,人们应用(联系人应用程序在一些旧版本)总是返回一个结果URI标识的内容选择接触,和相机应用程序返回一个位图在“数据”额外的(参见类对捕获的照片)。
好处:读取联系人数据
上面的代码展示了如何从人民得到一个结果应用不进入细节如何读取的数据结果,因为它需要更高级的讨论内容提供商。然而,如果你很好奇,这里有一些更多的代码显示了如何查询结果数据从选中的联系电话号码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request it is that we're responding to
if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// Get the URI that points to the selected contact
Uri contactUri = data.getData();
// We only need the NUMBER column, because there will be only one row in the result
String[] projection = {Phone.NUMBER};
// Perform the query on the contact to get the NUMBER column
// We don't need a selection or sort order (there's only one result for the given URI)
// CAUTION: The query() method should be called from a separate thread to avoid blocking
// your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
// Consider using CursorLoader
to perform the query.
Cursor cursor = getContentResolver()
.query(contactUri, projection, null, null, null);
cursor.moveToFirst();
// Retrieve the phone number from the NUMBER column
int column = cursor.getColumnIndex(Phone.NUMBER);
String number = cursor.getString(column);
// Do something with the phone number...
}
}
}
注意:在Android 2.3(API级别9)之前,执行一个查询联系人提供者(如上所示)的要求应用程序声明READ_CONTACTS许可(见安全与权限)。然而,从Android 2.3开始,联系人/人应用程序授予应用程序暂时允许读取联系人提供者当它返回一个结果。临时许可只适用于特定的联系请求,所以你不能查询另一个联系人指定目的的Uri,除非你做声明READ_CONTACTS许可。