跟Google学习Android开发-起始篇-与其它应用程序交互(2)

6.2从活动获取结果


启动另一个活动不必是单向的。您也可以启动另一个活动,并接收一个结果回来。为了接收一个结果,调用startActivityForResult()(而不是startActivity())。

例如,您的应用程序可以启动照相机应用程序,并接收拍摄的照片作为结果。或者,您可能启动通讯录应用程序,让用户选择一个联系人,您会收到联系人详情作为结果。

当然,响应的活动必须设计为返回一个结果。当它这样做时,它会发送结果作为另一个Intent对象。你的活动在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 whichrequest 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 whichcontact was selected.

            // Do something with the contact here(bigger example below)
        }
    }
}


在这个例子中, Android通讯录或联系人应用程式返回的结果意图提供了一个内容Uri标识用户选择的联系人。

为了成功地处理结果,你必须明白结果意图将会是什么格式 。当返回结果的活动是自己的活动之一时,这样做是很容易的。Android平台包含的应用程序提供自己的API,依靠这些API你可以得到特定的结果数据。例如,联系人应用程序(在一些比较旧的版本是通讯录应用程序)总是返回内容URI的结果,该URI标识选定的联系人,而相机应用程序在“data”extra中返回一个Bitmap(见捕捉照片一课)。


福利(Bonus):读取联系人数据

上面显示如何从联系人应用程序得到结果的代码并没有深入到如何实际从结果读取数据的细节,因为它需要关于内容提供者更先进的讨论。但是,如果你很好奇,下面就有更多的代码,显示了如何查询结果数据来获得选定的联系人的电话号码:


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check whichrequest 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 selectedcontact
            Uri contactUri = data.getData();
            // We only need the NUMBER column, becausethere will be only one row in the result
            String[] projection = {Phone.NUMBER};

            // Perform the query on the contact to getthe 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 becalled from a separate thread to avoid blocking
            // your app's UI thread. (For simplicity ofthe 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 theNUMBER column
            int column = cursor.getColumnIndex(Phone.NUMBER);
            String number = cursor.getString(column);

            // Do something with the phone number...
        }
    }
}


注:Android 2.3API级别9)之前,在联系供应(如上面所示)上进行查询,需要您的应用程序声明READ_CONTACTS权限(见安全性和权限)。然而,从Android 2.3开始,通讯录/联系人应用程式授予您的应用程序一个临时的从联系供应商读取的权限,当它返回你一个结果。临时权限仅适用于特定联系人的请求,所以你不能查询意图的Uri指定联系人以外的联系人,除非你的确声明了READ_CONTACTS权限。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值