两个APK之间进行通信

test01 跳到test02的activity中,并且传输数据.
test02获取数据 ,同时也可以返回数据给test01.


test01的activity:

  1. public class Test01Activity extends Activity {  
  2.     /** Called when the activity is first created. */  
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.main);  
  7.         Button button = (Button) findViewById(R.id.button);  
  8.         button.setOnClickListener(new OnClickListener() {  
  9.             public void onClick(View v) {  
  10.                 ComponentName componetName = new ComponentName(  
  11.                 // 这个是另外一个应用程序的包名  
  12.                         "cn.test02",  
  13.                         // 这个参数是要启动的Activity  
  14.                         "cn.test02.Test02Activity");  
  15.                 Intent intent = new Intent();  
  16.   
  17.                 intent.putExtra("data", "我是Test01Activity类");  
  18.                 intent.setComponent(componetName);  
  19.                 startActivityForResult(intent, 1);// 发送数据,并且可以获取返回数据  
  20.             }  
  21.         });  
  22.     }  
  23.   
  24.     /**  
  25.      * 获取返回数据的方法  
  26.      */  
  27.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  28.         super.onActivityResult(requestCode, resultCode, data);  
  29.         switch (requestCode) {  
  30.         case 1:  
  31.             if (resultCode == RESULT_OK) {  
  32.                 System.out.println("获取返回的数据= " + "   "  
  33.                         + data.getStringExtra("data02"));  
  34.             }  
  35.             break;  
  36.   
  37.         }  
  38.     }  
  39. }  
  40. <pre class="html" name="code"><pre class="html" name="code">test01的xml:  
  41.   
  42. <?xml version="1.0" encoding="utf-8"?>  
  43. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  44.     android:orientation="vertical"  
  45.     android:layout_width="fill_parent"  
  46.     android:layout_height="fill_parent"  
  47.     >  
  48. <TextView    
  49.     android:layout_width="fill_parent"   
  50.     android:layout_height="wrap_content"   
  51.     android:text="@string/hello"  
  52.     />  
  53.       
  54.     <Button android:id="@+id/button"  android:layout_width="fill_parent"   
  55.     android:layout_height="wrap_content" android:text="跳转到Test02项目的Test02Activity类" />  
  56. </LinearLayout>  
  57. </pre><br>  
  58. <br>  
  59. <pre></pre>  
  60. <pre></pre>  
  61. <br>  
  62. test02的activity<br>  
  63. <pre class="html" name="code">package cn.test02;  
  64.   
  65. import android.app.Activity;  
  66. import android.content.Intent;  
  67. import android.net.Uri;  
  68. import android.os.Bundle;  
  69. import android.view.View;  
  70. import android.view.View.OnClickListener;  
  71. import android.widget.Button;  
  72. import android.widget.TextView;  
  73.   
  74. public class Test02Activity extends Activity {  
  75.     /** Called when the activity is first created. */  
  76.     @Override  
  77.     public void onCreate(Bundle savedInstanceState) {  
  78.         super.onCreate(savedInstanceState);  
  79.         setContentView(R.layout.main);  
  80.   
  81.         // 改变文字内容,标志这是从activity跳转过来的  
  82.         TextView texView = (TextView) findViewById(R.id.text1);  
  83.         Intent intent = getIntent();  
  84.         String value = intent.getStringExtra("data");// 获取对应传过来的数据  
  85.         if (value != null && !value.equals("")) {  
  86.             texView.setText(value);// 这里将显示“这是跳转过来的!来自Jar02Activity  
  87.         } else {  
  88.             System.out.println("空的参数");  
  89.         }  
  90.   
  91.         // 当点击时返回数据给test01  
  92.         Button shuaka_button = (Button) findViewById(R.id.button);  
  93.         shuaka_button.setOnClickListener(new OnClickListener() {  
  94.             public void onClick(View v) {  
  95.                 Uri data = Uri.parse("one................");  
  96.                 Intent result = new Intent(null, data);  
  97.                 result.putExtra("data02", "我是来自test02的是数据");  
  98.                 setResult(RESULT_OK, result);  
  99.                 finish();  
  100.             }  
  101.         });  
  102.   
  103.     }  
  104. }  
  105.   
  106.   
  107. <pre class="html" name="code">test02 的xml:  
  108.   
  109. <?xml version="1.0" encoding="utf-8"?>  
  110. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  111.     android:orientation="vertical"  
  112.     android:layout_width="fill_parent"  
  113.     android:layout_height="fill_parent"  
  114.     >  
  115. <TextView  android:id="@+id/text1"  
  116.     android:layout_width="fill_parent"   
  117.     android:layout_height="wrap_content"  />  
  118.       
  119.     <Button android:id="@+id/button" android:layout_width="fill_parent"   
  120.     android:layout_height="wrap_content" android:text="返回数据给test01" />  
  121. </LinearLayout>  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值