启动其它Activity不是单向的。你也能启动其它Activity并获取一个返回结果。为了获取一个结果,调用startActivityForResult()方法(替代startActivity()方法)。
例如,你的应用程序能启动一个照相机应用程序,并获取捕捉的相片作为结果。或者,你可能启动People应用程序,为了用户选择一个联系人,并且你将会获取这个联系的的详细信息作为结果。
当然,这个响应Activity必须被设计来返回一个结果。当它执行的时候,它发送这个结果作为其它Intent对象。你的Activity在onActivityResult()回调方法中获取它。
注意:你能使用显示的或者隐式的意图,当你调用startActivityForResult()方法的时候。当启动一个你自己的Activity来获取一个结果的时候,你应该使用一个明确的意图来确保你获取获取期望的结果。
启动Activity
——————————————————————————————————————————————————————————————
当你启动一个Activity返回结果的时候,这里没有指定关于你使用Intent对象的任何事情,但是你需要传递一个额外的整型参数给startActivityForResult()方法。
这个整型参数是一个“请求码“,他确定你的请求。但你获取这个结果Intent的时候,回调方法提供了相同的访问码,以至于你的应用程序能正确的识别这个结果,并决定如何处理它。
例如,这里是如何启动一个Activity,它允许用户来获取一个联系人:
static final int PICK_CONTACT_REQUEST = 1; // The request code
...
private void pickContact() {
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, new Uri("content://contacts"));
pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}
获取结果
——————————————————————————————————————————————————————————————
当用户处理了后来的Activity并返回的时候,系统调用你的Activty的onActivityResult()方法。这个方法包含三个参数:
例如,这里是你如何处理这个”获取一个联系“意图的记过:
@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通过Andrid的Contacts或者People应用程序提供一个内容Uri被返回,它确定用户选择的联系人。
为了成功的处理这个结果,你必须懂得结果Intent的格式。当Activity返回的结果是一个你自己的Activity的时候是非常简单的。包含在Android平台中的应用程序支持你能指定特定结果数据的API。例如,People应用程序(联系人应用程序在一些老的版本)总是返回一个结果,伴随着content
URI,它指定了被选择的联系人,并且Camera应用程序在”data”额外数据中返回一个Bitmap(查看关于Capturing Photos的课程)。
上面显示的如何从People应用程序获取一个结果的代码,没有关于如何从这个结果中读取数据的详细说明,因为它需要在content provider中需要更详细的讨论。然而,如果你很好奇,这里有一些代码,它展示了如何查询从选择额联系人获取电话号码结果数据:
@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...
}
}
}
注意:在Andrid2.3之前(API
Level 9),在这个Content Provider中执行一个查询(想上上面显示的)要求你的应用程序声明READ_CONTACTS权限(查看Security and Permissions)。然而,从Android2.3开始,Contacts/People应用程序授予应用程序临时的权限,当它返回你一个结果的时候读取联系人Provider。这个临时的权限仅仅应用给指定的联系人请求,所以你不能查询一个其它联系人,除了通过Intent的Uri被指定,至少你要声明READ_CONTACTS权限。
分享到:
相关推荐
Android Cookbook: Problems and Solutions for Android Developers by Ian F. Darwin English | 10 May 2017 | ASIN: B071VLZ9ZK | 838 Pages | AZW3 | 12 MB Jump in and build working Android apps with the ...
Manning RxJava for Android Developers Manning RxJava for Android Developers
Kotlin for android developers 中文学习文档 https://leanpub.com/kotlin-for-android-developers 英文版 - Github: https://github.com/wangjiegulu/kotlin-for-android-developers-zh
Wiley - Java Programming for Android Developers - For Dummies 2nd - 2017
Kotlin for android developers中文版翻译 Kotlin for android developers中文版翻译
A useful tool for Android Developers, which shows the package name and class name of the current activity. To track the activities in shell you can run this shell script: current_activity.sh New ...
kotlin for android developers 中文版, kotlin for android developers 中文版
《Kotlin for android developers中文版翻译》
Linux for Developers: Jumpstart Your Linux Programming Skills (Developer's Library) by William "Bo" Rothwell English | 17 Apr. 2017 | ASIN: B06ZY85CPJ | 224 Pages | AZW3 | 5.37 MB Linux for ...
kotlin-for-android-developers-zh 中文版本,请支持正版。 https://leanpub.com/kotlin-for-android-developers
本书介绍Scala基本语法、构造。以及如何将您已经知道的复制java语法转换为Scala,了解Scala除了javabecome之外还提供了哪些功能编程概念和惯用技巧和建议,这些技巧和建议在将现有java项目转换为Scala时非常有用
kotlin-for-android-developers; kotlin-for-android-developers; kotlin-for-android-developers;
Kotlin for android developers(非扫描中文)下载后解压即可,更多请关注我的博客:xiayye5
Kotlin for Android Developers 中文版 ---------------------------------------------------- 本 ePub 基于开源文档,目录书签齐全。 版权归原作者,翻译版权归译者。 ------------------------------------------...
这个文档主要集中于和一个应用一样使用CalendarProviderAPI。查看同步Adapter的差异讨论,查阅SyncAdapters。通常地,读或者写日历数据,一个应用程序的清单文件必须包含相应的权限,在UserPermission中被描述。为了...
在这本书中,我会使用Kotlin作为主要的语言来开发一个android应用。方式是通过 开发一个应用来学习这门语言,而不是根据传统的结构来学习。我会在感兴趣的点 停下来通过与Java1.7对比的方式讲讲Kotlin的一些概念和...
Kotlin for Android Developers - Learn Kotlin the easy way.pdf
Kotlin-for-Android-Developers:该书的配套应用
Android Programming for Developers 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书