test01 跳到test02的activity中,并且传输数据.
test02获取数据 ,同时也可以返回数据给test01.
test01的activity:
- public class Test01Activity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button button = (Button) findViewById(R.id.button);
- button.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- ComponentName componetName = new ComponentName(
- // 这个是另外一个应用程序的包名
- "cn.test02",
- // 这个参数是要启动的Activity
- "cn.test02.Test02Activity");
- Intent intent = new Intent();
- intent.putExtra("data", "我是Test01Activity类");
- intent.setComponent(componetName);
- startActivityForResult(intent, 1);// 发送数据,并且可以获取返回数据
- }
- });
- }
- /**
- * 获取返回数据的方法
- */
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- switch (requestCode) {
- case 1:
- if (resultCode == RESULT_OK) {
- System.out.println("获取返回的数据= " + " "
- + data.getStringExtra("data02"));
- }
- break;
- }
- }
- }
- <pre class="html" name="code"><pre class="html" name="code">test01的xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- <Button android:id="@+id/button" android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:text="跳转到Test02项目的Test02Activity类" />
- </LinearLayout>
- </pre><br>
- <br>
- <pre></pre>
- <pre></pre>
- <br>
- test02的activity<br>
- <pre class="html" name="code">package cn.test02;
- import android.app.Activity;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- public class Test02Activity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- // 改变文字内容,标志这是从activity跳转过来的
- TextView texView = (TextView) findViewById(R.id.text1);
- Intent intent = getIntent();
- String value = intent.getStringExtra("data");// 获取对应传过来的数据
- if (value != null && !value.equals("")) {
- texView.setText(value);// 这里将显示“这是跳转过来的!来自Jar02Activity
- } else {
- System.out.println("空的参数");
- }
- // 当点击时返回数据给test01
- Button shuaka_button = (Button) findViewById(R.id.button);
- shuaka_button.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- Uri data = Uri.parse("one................");
- Intent result = new Intent(null, data);
- result.putExtra("data02", "我是来自test02的是数据");
- setResult(RESULT_OK, result);
- finish();
- }
- });
- }
- }
- <pre class="html" name="code">test02 的xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView android:id="@+id/text1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- <Button android:id="@+id/button" android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:text="返回数据给test01" />
- </LinearLayout>