博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Getting a Result from an Activity
阅读量:4212 次
发布时间:2019-05-26

本文共 1019 字,大约阅读时间需要 3 分钟。

一般情况下我们会通过startActivity()来启动一个activity,但是如果我们想获得要启动的activity的结果的时候就需要调用startActivityForResult().并且重写 onActivityResult() 。
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);
}
得到结果
@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)
        }
    }
}
注意要判断requestCode == PICK_CONTACT_REQUEST 且resultCode == RESULT_OK。
如上所示:onActivityResult 是用Intent 来返回数据的。这个时候我们就可以通过Intent的getData()来获得数据.

转载地址:http://vvcmi.baihongyu.com/

你可能感兴趣的文章
如何修改Eclipse的 workspace目录
查看>>
.pvr.ccz 与 png 格式 互转的解决方案
查看>>
将Texture Packer制作的.pvr.ccz和.plist文件还原为多个原图
查看>>
将Texture Packer制作的.pvr.ccz和.plist文件还原为多个原图--格式之后
查看>>
PIL The _imaging C module is not installed
查看>>
将Texture Packer制作的.pvr.ccz和.plist文件还原为多个原图 -- 使用说明文档
查看>>
将Texture Packer制作的.pvr.ccz和.plist文件还原为多个原图 -- 可以一下转化多个
查看>>
查看Eclipse版本
查看>>
[cocos2d-x] --- 使用位图工具自定义字体
查看>>
BMFont中文字体图集制作的方法~(for unity ngui)
查看>>
cocos2dx添加文本的三种方法及适用情况CCLabelTTF,CCLabelBMFont,CCLabelAtlas
查看>>
libgdx下Texture packer工具使用
查看>>
libgdx: 2D Particle Editor工具使用
查看>>
hiero使用
查看>>
libgdx:Hiero工具使用
查看>>
libgdx:学习网站或博客
查看>>
eclipse 给jar库添加源码
查看>>
cocos2d-x 3.0rc开发指南:Windows下Android环境搭建
查看>>
3.0正式版环境搭建(1)-- 运行自带例子
查看>>
3.0正式版环境搭建(2)-- 环境变量设置
查看>>