接收Activity返回的结果

编写:kesenhoo - 原文:http://developer.android.com/training/basics/intents/result.html

启动另外一个activity并不一定是单向的。我们也可以启动另外一个activity然后接受一个返回的result。为接受result,我们需要使用startActivityForResult() ,而不是startActivity()

例如,我们的app可以启动一个camera程序并接受拍的照片作为result。或者可以启动联系人程序并获取其中联系的人的详情作为result。

当然,被启动的activity需要指定返回的result。它需要把这个result作为另外一个intent对象返回,我们的activity需要在onActivityResult()的回调方法里面去接收result。

Note:在执行startActivityForResult()时,可以使用explicit 或者 implicit 的intent。当启动另外一个位于的程序中的activity时,我们应该使用explicit intent来确保可以接收到期待的结果。

启动Activity

对于startActivityForResult() 方法中的intent与之前介绍的并无太大差异,不过是需要在这个方法里面多添加一个int类型的参数。

该integer参数称为”request code”,用于标识请求。当我们接收到result Intent时,可从回调方法里面的参数去判断这个result是否是我们想要的。

例如,下面是一个启动activity来选择联系人的例子:

  1. static final int PICK_CONTACT_REQUEST = 1; // The request code
  2. ...
  3. private void pickContact() {
  4. Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
  5. pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
  6. startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
  7. }

接收Result

当用户完成了启动之后activity操作之后,系统会调用我们activity中的onActivityResult() 回调方法。该方法有三个参数:

  • 通过startActivityForResult()传递的request code。
  • 第二个activity指定的result code。如果操作成功则是RESULT_OK ,如果用户没有操作成功,而是直接点击回退或者其他什么原因,那么则是RESULT_CANCELED
  • 包含了所返回result数据的intent。

例如,下面显示了如何处理pick a contact的result:

  1. @Override
  2. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  3. // Check which request we're responding to
  4. if (requestCode == PICK_CONTACT_REQUEST) {
  5. // Make sure the request was successful
  6. if (resultCode == RESULT_OK) {
  7. // The user picked a contact.
  8. // The Intent's data Uri identifies which contact was selected.
  9. // Do something with the contact here (bigger example below)
  10. }
  11. }
  12. }

本例中被返回的Intent使用Uri的形式来表示返回的联系人。

为正确处理这些result,我们必须了解那些result intent的格式。对于自己程序里面的返回result是比较简单的。Apps都会有一些自己的api来指定特定的数据。例如,People app (Contacts app on some older versions) 总是返回一个URI来指定选择的contact,Camera app 则是在data数据区返回一个 Bitmap (see the class about Capturing Photos).

读取联系人数据

上面的代码展示了如何获取联系人的返回结果,但没有说清楚如何从结果中读取数据,因为这需要更多关于content providers的知识。但如果想知道的话,下面是一段代码,展示如何从被选的联系人中读出电话号码。

  1. @Override
  2. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  3. // Check which request it is that we're responding to
  4. if (requestCode == PICK_CONTACT_REQUEST) {
  5. // Make sure the request was successful
  6. if (resultCode == RESULT_OK) {
  7. // Get the URI that points to the selected contact
  8. Uri contactUri = data.getData();
  9. // We only need the NUMBER column, because there will be only one row in the result
  10. String[] projection = {Phone.NUMBER};
  11. // Perform the query on the contact to get the NUMBER column
  12. // We don't need a selection or sort order (there's only one result for the given URI)
  13. // CAUTION: The query() method should be called from a separate thread to avoid blocking
  14. // your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
  15. // Consider using CursorLoader to perform the query.
  16. Cursor cursor = getContentResolver()
  17. .query(contactUri, projection, null, null, null);
  18. cursor.moveToFirst();
  19. // Retrieve the phone number from the NUMBER column
  20. int column = cursor.getColumnIndex(Phone.NUMBER);
  21. String number = cursor.getString(column);
  22. // Do something with the phone number...
  23. }
  24. }
  25. }

Note:在Android 2.3 (API level 9)之前对Contacts Provider的请求(比如上面的代码),需要声明READ_CONTACTS权限(更多详见Security and Permissions)。但如果是Android 2.3以上的系统就不需要这么做。但这种临时权限也仅限于特定的请求,所以仍无法获取除返回的Intent以外的联系人信息,除非声明了READ_CONTACTS权限。