得到另一个activity传递过来的数据

启动另一个activity 可以调用startActivity

也可以用startActivityForResult 启动另一个activity,当另一个activity关闭时候,可以获得

从这个activity中获得一些数据。

例如:你的app可以启动一个照相的功能,获得一张照片作为结果,传入你的app,或者打开通讯录获得一个联系人。

当然你通过startActivityForResult启动的activity,被设计成返回一个结果。存储到intent对象中,在你的activity中通过onActivityResult方法获取结果。

 

startActivityForResult 方法中,需要一个整数参数 请求码,当你收到传来的intent,通过验证请求码来验证结果是否是你需要的,然后处理他。

下面代码是获取一条通讯录的联系人信息:

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收到结果:这个函数有三个参数:

1.你的请求码 startActivityForResult的参数

2.被你启动的Activity所传回来的结果码,在上个例子中就是,通讯录activity传来的结果码,系统的就是RESULT_OK 操作成功,或者RESULT_CANCELED操作取消或者失败的情况。

3.第三个参数就是 存取数据的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)
        }
    }
}

   如果你想成功处理返回来的结果你必须了解intent结果类型。如果是你自己写的activity 那会很好处理,系统的你就需要了解特定的类型。例如通讯录会返回 content URI ,照相 会返回一个Bitmap对象在你的intent中。

下面代码显示如何获取联系人的信息

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

 

注意:android2.3 api 9)之前,利用content Provider 读取联系人信息,需要声明权限,READ_CONTACTS. android 2.3开始,可以声明一个临时的权限,去读取通讯录,这个读取。使用户去选择一个联系人,返回你要的结果,如果你要读取其他联系人的信息,还是需要权限的。

 

对于你自己定义的activity,得到你想要的结果,首先调用startActivityForResult你自己的activity,在你打开的activity中 调用setResult方法传入绑定数据的intent

      setResult(RESULT_OK, intent); 

下面记住只有调用了 finish方法结束了,你现在的actvity,上一个activity才会调用onActvityForResult方法,并且得到数据。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值