Getting a Result from an Activity

来源:互联网 发布:c语言 表单解析 编辑:程序博客网 时间:2024/05/04 11:44

原文点击打开链接

Getting a Result from an Activity



启动另一个活动不一定是单向的。你也可以开始另一个活动和接收一个返回结果。获得结果,


调用startActivityForResult()(而不是startActivity())。


例如,您的应用程序就可以开始一个摄像头应用程序和接收捕获的照片。或者,你可能会开始一


个联系人app为了让用户选择一个联系人,你会收到联系详细信息。


当然,活动,响应必须返回一个结果。当它返回时,它将以一个意图对象为结果。你的活动收到


onActivityResult()回调。


注意:您可以使用显式或隐式意图当你叫startActivityForResult()。当开始你自己的活动中


的一个并获得返回结果,您应该使用一个明确的目的,以确保你得到预期的结果。


Start the Activity



你开始一个要结果意图,这个意图没什么特殊,但是你需要通过一个额外的整数参数给
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);
}




Receive the Result



当用户处理后续活动和返回,系统调用活动的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的联系人返回结果intent或我们的联系人app提供一个uri,这个uri指定了用户选择的联系人。


为了成功地处理结果,您必须理解结果意图的格式。应用程序中包含的Android平台提供自己的api,你可以指望得到特定的结果数据。例如,联系人应用总是返回一个内容uri的结果,这个uri识别选择的联系人。


Bonus: Read the contact data



上面的代码展示了如何从联系人应用获取结果而没有如何实际读取的数据结果的细节,因为它需要更高级的讨论内容提供着。然而,如果你好奇,这里有一些更多的代码显示了如何查询结果数据得到选中的联系电话号码:
@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许可。

0 0
原创粉丝点击