您的位置:首页 > 移动开发 > Android开发

Android培训翻译_从Activity获得结果

2012-08-21 16:27 344 查看

This lesson teaches you to

Start the Activity 启动Activity

Receive the Result 接收结果

You should also read

Sharing Content 共享内容

Starting another activity doesn't have to be one-way. You can also start another activity and receive a result back. To receive a result, call
startActivityForResult()
(instead of
startActivity()
).
启动另一个activity不一定是单向的,你也可以启动别的activity并接收其返回的结果。要接收结果,调用 startActivityForResult(),而非 startActivity()。

For example, your app can start a camera app and receive the captured photo as a result. Or, you might start the People app in order for the user to select a contact and you'll receive the contact details as a result.
例如,你的应用可以启动一个摄像头程序并将捕获的照片作为结果接收。或者,你可能启动联系人程序帮用户选择一个联系方式,你会将联系方式的细节作为结果接收。

Of course, the activity that responds must be designed to return a result. When it does, it sends the result as another
Intent
object. Your activity receives it in the
onActivityResult()
callback.
当然,该Activity的响应必须设计为可以返回结果。这么做时,activity将会以另一个Intent对象作为结果发送。你的activity 在 onActivityResult() 回调方法中接收这个对象。

Note: You can use explicit or implicit intents when you call
startActivityForResult()
. When starting one of your own activities to receive a result, you should use an explicit intent to ensure that you receive the expected result.
:调用 startActivityForResult() 时,你可以使用显式或隐式意图。启动你自己的一个activity接收结果时,你应当使用显式意图以确保你收到了预期的结果。

Start the Activity
启动Activity

There's nothing special about the
Intent
object you use when starting an activity for a result, but you do need to pass an additional integer argument to the
startActivityForResult()
method.
为了结果而启动activity的Intent对象并没什么特别 ,但你需要传递一个额外的整型参数到 startActivityForResult() 方法。

The integer argument is a "request code" that identifies your request. When you receive the result
Intent
, the callback provides the same request code so that your app can properly identify the result and determine how to handle it.

这个整型参数是标识你请求的“请求码”。当你接收Intent结果时,回调方法提供相同的请求码,这样你的应用就可以识别出结果并决定如何处理它。

For example, here's how to start an activity that allows the user to pick a contact:
例如下面,如何启动一个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);
}


Receive the Result
接收结果

When the user is done with the subsequent activity and returns, the system calls your activity's
onActivityResult()
method. This method includes three arguments:

当用户后续的activity已经完成并返回时,系统调用你activity的 onActivityResult() 方法。这个方法包含三个参数:

The request code you passed to
startActivityForResult()
.

你传递到 startActivityForResult()中的请求码

A result code specified by the second activity. This is either
RESULT_OK
if the operation was successful or
RESULT_CANCELED
if the user backed out or the operation failed for some reason.

第二个activity指定的结果码。操作成功时是 RESULT_OK ,用户退出或操作因为一些原因失败时则为RESULT_CANCELED。

An
Intent
that carries the result data.

携带结果数据的Intent

For example, here's how you can handle the result for the "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.
// 意图的Uri数据标识出被选择的联系人

// Do something with the contact here (bigger example below)
// 这里使用联系人做些什么(下面有个更大的例子)
}
}
}


In this example, the result
Intent
returned by Android's Contacts or People app provides a content
Uri
that identifies the contact the user selected.
在这个例子中,通过Android的联系人或人际应用程序返回的意图结果,提供了一个联系人的Uir,标识了用户所选择的那个联系人。

In order to successfully handle the result, you must understand what the format of the result
Intent
will be. Doing so is easy when the activity returning a result is one of your own activities. Apps included with the Android platform offer their own APIs that you can count on for specific result data. For instance, the People app (Contacts app on some older versions) always returns a result with the content URI that identifies the selected contact, and the Camera app returns a
Bitmap
in the
"data"
extra (see the class about Capturing Photos).

为了成功处理结果,你必须理解Intent结果将会是什么格式。当返回结果的Activity是你自己的Activity时,这么做很简单。应用程序包含了Android平台所提供的API,这样你可以期望特定的结果数据。举个例子,人际(People)应用程序(一些老版本上是联系人)总是返回一个有内容URI的结果,它标识了被选的联系人,而相机程序返回一个位图,在额外“数据”中(见有关拍摄照片的课程)

Bonus: Read the contact data

福利:读取联系人数据


The code above showing how to get a result from the People app doesn't go into details about how to actually read the data from the result, because it requires more advanced discussion about content providers. However, if you're curious, here's some more code that shows how to query the result data to get the phone number from the selected contact:

上面展示如何从人际程序中获取结果的代码没有关于如何从结果中读取数据的细节,因为需要更深入的讨论 内容提供者。然而,如果你好奇,这里有些展示如何查询结果数据的代码,从被选联系人获取电话号码。

@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
Uri contactUri = data.getData();
// We only need the NUMBER column, because there will be only one row in the result
// 我们只需要NUMBER列,因为结果中只会有一行。
String[] projection = {Phone.NUMBER};

// Perform the query on the contact to get the NUMBER column
// 在联系人上执行查询得到 NUMBER 列
// We don't need a selection or sort order (there's only one result for the given URI)

// 我们不需要选择或者排序(所给的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.)
// 注意:为了避免阻塞你的应用程序线程,query()方法应当在独立的线程中被调用(简单起见,我们不这么做)
// Consider using CursorLoader to perform the query.
// 考虑使用CursorLoader执行查询
Cursor cursor = getContentResolver()
.query(contactUri, projection, null, null, null);
cursor.moveToFirst();

// Retrieve the phone number from the NUMBER column
// 从NUMBER列中检索电话号码
int column = cursor.getColumnIndex(Phone.NUMBER);
String number = cursor.getString(column);

// Do something with the phone number...
// 用电话号码做些什么...
}
}
}


Note: Before Android 2.3 (API level 9), performing a query on the
Contacts Provider
(like the one shown above) requires that your app declare the
READ_CONTACTS
permission (see Security and Permissions). However, beginning with Android 2.3, the Contacts/People app grants your app a temporary permission to read from the Contacts Provider when it returns you a result. The temporary permission applies only to the specific contact requested, so you cannot query a contact other than the one specified by the intent's
Uri
, unless you do declare the
READ_CONTACTS
permission.

:在Android 2.3(API 9级)之前,在Contacts Provider上执行查询(像上面所示的那个)要求你的应用程序声明 READ_CONTACTS 权限(见安全与权限)。然而,从Android 2.3开始,联系人或人际应用程序返回你一个结果时,授予你的程序一个临时权限来从Contacts Provider读取数据。 临时权限只适用于特定的联系人请求,因此你不能查询意图Uri所指定的联系人之外的其它联系人,除非你有声明 READ_CONTACT 权限。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐