(一)开始____7、与其他的App交互——从另一个Activity中取回反馈结果-Getting a Result from the Activity

负责人:小手冰凉

原文链接:http://developer.android.com/training/basics/intents/result.html

目录

 [隐藏

Getting a Result from the Activity

启动另一个Activity不止一种方法。你可以启动一个Activity并且接收一个返回结果。为了接收该结果,我们可以调用 startActivityForResult()方法(取代原来的startActivity())。

例如:你的应用程序可以启动一个相机应用,将获得的照片作为结果返回回来。或者,在用户启动联系人程序选择了一个联系人时,将获得的联系人细节作为结果返回回来。

当然,上述启动的Activity一定要设计成有结果可返回的。这样,它将返回的结果作为一个Intent对象发送出去。你的Activity可以在回调函数 onActivityResult()中接收该对象。

注意: 你可以在调用startActivityForResult()方法时使用显式或者隐式的intents.
在启动一个属于你的那些Activities并接收结果时,你必须使用一个显式的intent来确保接收到期望的结果。

启动Activity

在上述这类Activity启动中使用的Intent对象并没有什么特别的。但是你需要向startActivityForResult()方法传递一个额外的整型参数。 这是一个可以定义你请求的请求码参数。当收到Intent结果时,回调方法提供了相同的请求码,使您的应用程序可以正确识别结果并处理它。

例如:这里就是如何启动一个Activity让用户选择一个联系人。

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

接收返回结果

当后续的Activity执行完成并返回结果时,系统将调用前面Activity的 onActivityResult()方法。该方法包含三个参数:

  • 你传递给 startActivityForResult()的请求码。
  • 由后续Activity指定的结果代码。若操作成功,其值为RESULT_OK。若由于某些原因,用户退出或者操纵失败,其值为RESULT_CANCELED。
  • 携带着返回数据的Intent.

例如:

@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的联系人应用程序返回的Intent结果提供了一个内容URI标识选定用户的联系人。

为了成功地处理结果,你必须明白作为结果返回的Intent将是什么格式 。当返回结果的Activity是自己所写的Activity时,这些确认是很容易的。Android平台里的应用程序提供了自己的APIs,你可以依赖其具体的结果数据。例如,联系人程序始终返回选定联系人的内容URI标识,相机应用程序返回一个位图。

读取联系人数据

上面这段展示如何从联系人应用中获得返回结果的代码并没有涉及到具体的获得细节,因为那里需要一些有关content providers的高级知识。 但是,如果你仍然想钻研细节,可以参考下面的代码,它展示了如何从返回结果中得到手机号。

@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 level 9)之前,执行Contacts Provider上的查询 (例如上面代码的功能),需要你的应用声明了READ_CONTACTS权限 (详见 Security and Permissions)。但是,从Android 2.3开始,联系人信息在返回结果时可以授予你暂时的读取权限,但这个临时权限仅适用于所请求的特定联系人,除非你声明READ_CONTACTS权限,否则不能查询一个在那个intent Uri定义之外的联系人信息。

posted @ 2014-07-29 16:52  ╰→劉じ尛鶴  阅读(132)  评论(0)    收藏  举报