从启动的activity中获得结果

  • 启动另一个activity总不是单程的。你可以启动另一个activity并且接收回一个结果。为了接收结果,调用startActivityForResult()(而不是startActivity())。
    比如:你的app可以启动一个camera app并且接收获取的图片作为返回结果。或者你可以打开联系人的app并且选择一个联系人然后你可以接收到联系人详细信息作为返回结果。
  • 当然,响应的activity也应该设计成能够返回一个结果的。如果真是这样的话,它将会发送这个结果作为另一个intent对象。你的activity在onActivityResult()回调函数红接收结果。
    注意:你可以使用explicit或者implicit intent当你调用startActivityForResult()函数时。当你启动自己的activities来接收结果,你应该使用explicit intent来确保你能够接收到想要的结果。

启动activity

使用Intent对象来启动一个需要返回结果的activity并没有特别之处。但是你需要传递另个额外的integer参数到startActivityForResult()中。这个参数是来标志你的request的“request code”。当你接收到result Intent,回调函数会提供一个相同的request code,这样一来,你的app可以准确的定位到result并且决定如何处理。
以下是启动一个允许用户选择联系人的activity

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);
} 

接收结果

  • 当返回结果,系统会调用activity的onActivityResult()函数。这个函数包含以下三个参数。
  • 传递到startActivityForResult()函数中的request code
  • 第二个activity确定的result code。可能是RESULT_OK(如果操作成功)或者是RESULT_CANCELED(如果用户退出或者操作失败)
  • 一个携带着结果数据的Intent
    比如:以下是你如何处理“pick a contact”的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) 
        } 
    } 
}

在这个例子中,result Intent是被android的联系人或者其他的app返回的。并且提供了一个Uri来确定用户选择的联系人。

  • 为了能够成功地处理这个result,你必须理解这个result Intent会是什么样的形式。如果是app自己的activities返回的结果会很容易知道。你可以依赖android平台内建的apps的自己的APIs来确定结果数据。比如,People app(老版本是Contacts app)总是返回一个标志选中的联系人的Content URI作为结果,Camera app返回一个Bitmap
福利:读取contact 数据
  • 上面的代码只是展示如何从People app中获取一个结果,但是没有展示如何从结果中读取数据,因为需要更多关于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... 
        } 
    } 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值