从其他Activity中获取结果Result

1:使用startActivityForResult()而不是startActivity()启动另外一个Activity
方法的签名是:startActivityForResult(Intent intent, int requestCode)这个方法有几个重载的方法
注意:
传入的Intent可以是隐式Intent或者是显示Intent。但如果是启动一个你自己的Activity,则最好使用显示Intent
在调用 startActivityForResult()时传入一个请求码(这个请求码用来识别请求,即返回的结果是由那个Activity中返回的),当返回结果时,这个请求码会被传回来
2:重写onActivityResult()这个方法接收返回的结果
方法的签名是:onActivityResult(int requestCode, int resultCode, Intent data)
第一个参数:startActivityForResult()方法执行时传入的请求码(请求码如果小于0,则和startActivity()方法一样,是不返回结果的)
第二个参数:返回操作的结果码,如果操作成功返回RESULT_OK,如果操作失败返回RESULT_CANCELED,例如用户自己回退(按BACK键)或其他原因失败
第三个参数:返回的结果,为了处理这个结果,你必须知道返回的Intent的格式。其中Android平台自带的Apps都有相应的API描述指定的结果数据
例子(摘自Android官网http://developer.android.com/training/basics/intents/result.html):

 1     package com.example.androidgov;
 2 
 3     import android.app.Activity;
 4     import android.content.Intent;
 5     import android.database.Cursor;
 6     import android.net.Uri;
 7     import android.os.Bundle;
 8     import android.provider.ContactsContract.CommonDataKinds.Phone;
 9     import android.view.View;
10     import android.widget.TextView;
11 
12     public class ReturnResultActivity extends Activity {
13 
14         static final int PICK_CONTACT_REQUEST = 1; // The request code
15         private TextView rra_textview = null;
16         
17         @Override
18         protected void onCreate(Bundle savedInstanceState) {
19             super.onCreate(savedInstanceState);
20             setContentView(R.layout.activity_return_result);
21             rra_textview = (TextView)findViewById(R.id.rra_textview1);
22         }
23 
24         public void pickContact( View view ) {
25          Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
26          pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
27          startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
28         }
29         
30         @Override
31         protected void onActivityResult(int requestCode, int resultCode, Intent data) {
32          // Check which request it is that we're responding to
33          if (requestCode == PICK_CONTACT_REQUEST) {
34          // Make sure the request was successful
35          if (resultCode == RESULT_OK) {
36          // Get the URI that points to the selected contact
37          Uri contactUri = data.getData();
38          // We only need the NUMBER column, because there will be only one row in the result
39          String[] projection = {Phone.NUMBER};
40 
41          // Perform the query on the contact to get the NUMBER column
42          // We don't need a selection or sort order (there's only one result for the given URI)
43          // CAUTION: The query() method should be called from a separate thread to avoid blocking
44          // your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
45          // Consider using CursorLoader to perform the query.
46     //这个查询动作应该放到另外一个线程中,以防止阻塞UI线程,另外可以考虑使用CursorLoader来执行查询
47          Cursor cursor = getContentResolver()
48          .query(contactUri, projection, null, null, null);
49          cursor.moveToFirst();
50 
51          // Retrieve the phone number from the NUMBER column
52          int column = cursor.getColumnIndex(Phone.NUMBER);
53          String number = cursor.getString(column);
54          rra_textview.setText("You choose " + number);
55          // Do something with the phone number...
56          }
57          }
58         }
59     }
View Code

布局文件为:

 1     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2         xmlns:tools="http://schemas.android.com/tools"
 3         android:id="@+id/container"
 4         android:layout_width="match_parent"
 5         android:layout_height="match_parent"
 6         android:orientation="vertical"
 7         tools:context="com.example.androidgov.ReturnResultActivity" >
 8 
 9         <Button
10             android:layout_width="match_parent"
11             android:layout_height="wrap_content"
12             android:onClick="pickContact"
13             android:text="GetContact" />
14 
15         <TextView
16             android:id="@+id/rra_textview1"
17             android:layout_width="match_parent"
18             android:layout_height="wrap_content" />
19 
20     LinearLayout>
activity_return_result

运行结果截图:

从其他Activity中获取结果Result_第1张图片

选择联系人后:

从其他Activity中获取结果Result_第2张图片

 

转载于:https://www.cnblogs.com/creasylai19/p/3771509.html

你可能感兴趣的:(从其他Activity中获取结果Result)