现在的位置: 首页 > 综合 > 正文

android开发实战系列(20)– 从另一个Activity中取回反馈结果

2013年12月08日 ⁄ 综合 ⁄ 共 3188字 ⁄ 字号 评论关闭

启动一个Activity不仅仅有一种方法。我们可以启动另外一个Activity和接收一个返回的结果。为了接收返回的结果,我们可以调用 startActivityForResult()方法。

例如:你的应用程序可以启动一个相机应用,接收返回的照片作为结果。或者,你可以启动联系人应用为了在联系人中查找一个用户,你会收到联系人的详细作为结果。

当然,Activity的响应一定要被设计有结果返回。当是这样的话,它发送结果给另外一个Intent对象。你的Activity在回调函数 onActivityResult()中接收返回结果。

Note: You can use explicit or implicit intents when you call startActivityForResult(). When starting one of your own activities to receive a result, you should use an explicit intent to ensure that you receive the expected result

启动Activity


你使用的Intent对象没有什么特别的,当你启动一个Activity需要结果返回时。当收到Intent的结果时,回调提供了相同的请求的代码,使您的应用程序可以正确识别结果,并决定如何处理它。

例如:这里就是如何启动一个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, new Uri("content://contacts"));
  5. pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
  6. startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
  7. }

接收返回结果


当后续的Activity执行完成后并返回,系统讲调用Activity的 onActivityResult()方法。这个方法包括三个参数:

    • 请求的代码,你传递给 startActivityForResult()。
    • 由第二个Activity指定的结果代码。如果操作成功了就是RESULT_OK,由于某种原因,用户备份或者操纵失败,返回结果是RESULT_CANCELED。
    • 携带着结果代码的Intent.

例如:

  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. }

在这个例子中,Android的联系人应用程序返回的Intent结果提供了一个内容URI标识用户选定的联系人。

为了成功地处理结果,你必须明白Intent的结果将是什么格式 。这样做是很容易的,当返回结果的Activity是自己的ACtivity之一。应用程序包括Android平台提供自己的API,你可以指定具体的结果数据。例如,联系人应用程序始终返回选定联系人的内容URI标识,相机应用程序返回一个位图。

读取联系人数据

有关如何从结果中读取数据的详细信息,在上面的代码中展示了如何从联系人的应用程序中读取联系人的数据。下面的代码展示了如何从选顶的联系人的查询结果数据中获取用户的电话号码。

  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: Before Android 2.3 (API level 9), performing a query on the Contacts Provider (like the one shown above) requires that your app declare the READ_CONTACTS permission (see Security and Permissions). However, beginning with Android 2.3,
the Contacts/People app grants your app a temporary permission to read from the Contacts Provider when it returns you a result. The temporary permission applies only to the specific contact requested, so you cannot query a contact other than the one specified
by the intent's Uri, unless you do declare the READ_CONTACTS permission.

抱歉!评论已关闭.