?
?
网上看了小例子、
现在自己整理一下子:
?
现在想实现一个
Activity1?? 调用?? Activity2?? 然后 Activity2返回数据给Activity1
?
流程:
1、Activity1 启动Activity2? 设置返回码和重写onActivityResult
2、Activity2 完成操作后 返加activity 并传入值
3、Activity1 onActivityResult 里就可以得到Activity2传进来的值。
?
例子:选择电话号码并返回例子。
?
Activity1
layout? layout_activity_return_test.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/showTextViewId" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onclick" android:text="返回数据" /></LinearLayout>
?Activity IntentReturnTest.java
package com.main;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.TextView;public class IntentReturnTest extends Activity { private TextView text; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_activity_return_test); text = (TextView) findViewById(R.id.showTextViewId); } public void onclick(View view){ Intent intent = new Intent(); intent.setClass(IntentReturnTest.this, GetContentResolverActivity.class); startActivityForResult(intent, 1);//返回时提供识别 } protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch(requestCode){ case 1: text.setText("---|"+data.getStringExtra("msg")+"---|"); break; } }}
?
?
Activity2
layout?? layout_get_content_resolver.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@+id/showContentListView" android:layout_height="wrap_content" android:layout_width="fill_parent" > </ListView></LinearLayout>?
Activity2 类
package com.main;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.content.Intent;import android.database.Cursor;import android.os.Bundle;import android.provider.Contacts;import android.util.Log;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import android.widget.ListView;public class GetContentResolverActivity extends Activity { private List<String> items; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_get_content_resolver); ListView listView = (ListView) findViewById(R.id.showContentListView); items = new ArrayList<String>(); Cursor cursor = getContentResolver().query(Contacts.Phones.CONTENT_URI,null,null,null,null); while(cursor.moveToNext()){ String name = cursor.getString(cursor.getColumnIndex(Contacts.Phones.NAME)); String number = cursor.getString(cursor.getColumnIndex(Contacts.Phones.NUMBER)); items.add(name+":"+number); } cursor.close(); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 ,items); listView.setAdapter(adapter); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Log.i("test", items.get(position)); Intent intent = getIntent(); intent.putExtra("msg", items.get(position)); GetContentResolverActivity.this.setResult(RESULT_OK,intent); GetContentResolverActivity.this.finish(); } }); }}?
?
添加权限
<!-- 读取联系人权限 --> <uses-permission android:name="android.permission.READ_CONTACTS" />
?
?
注册Activity
<application android:icon="@drawable/image_button" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".IntentReturnTest" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".GetContentResolverActivity" > </activity> </application>?
?
好ok 完成 。
附件 apk
?
?
?
?