从另一个Activity中取回反馈结果
负责人:小手冰凉
http://docs.eoeandroid.com/training/basics/intents/result.html
http://training.eoeandroid.com/#Getting_a_Result_from_the_Activity
目录 |
Getting a Result from the Activity
启动一个Activity不仅仅有一种方法。我们可以启动另外一个Activity和接收一个返回的结果。为了接收返回的结果,我们可以调用 startActivityForResult()方法。
例如:你的应用程序可以启动一个相机应用,接收返回的照片作为结果。或者,你可以启动联系人应用为了在联系人中查找一个用户,你会收到联系人的详细作为结果。
当然,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
启动Activity
你使用的Intent对象没有什么特别的,当你启动一个Activity需要结果返回时。当收到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执行完成后并返回,系统讲调用Activity的 onActivityResult()方法。这个方法包括三个参数:
- 请求的代码,你传递给 startActivityForResult()。
- 由第二个Activity指定的结果代码。如果操作成功了就是RESULT_OK,由于某种原因,用户备份或者操纵失败,返回结果是RESULT_CANCELED。
- 携带着结果代码的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 } } }
在这个例子中,Android的联系人应用程序返回的Intent结果提供了一个内容URI标识用户选定的联系人。
为了成功地处理结果,你必须明白Intent的结果将是什么格式 。这样做是很容易的,当返回结果的Activity是自己的ACtivity之一。应用程序包括Android平台提供自己的API,你可以指定具体的结果数据。例如,联系人应用程序始终返回选定联系人的内容URI标识,相机应用程序返回一个位图。
读取联系人数据
有关如何从结果中读取数据的详细信息,在上面的代码中展示了如何从联系人的应用程序中读取联系人的数据。下面的代码展示了如何从选顶的联系人的查询结果数据中获取用户的电话号码。
@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... } } }
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.