主题:利用Bundle实现Android Activity间消息的传递

之前写过一篇文章是关于Android 的Activity跳转 的,但当中没涉及到Activity的消息的传递。利用Bundle 是一种比较方便的办法。

程序的效果是活动A向B跳转的同时发送一字符串,B读出字符串。

先在AndroidManifest.xml中定义一个新的Activity,target:

Xml代码
  1. <? xml   version = "1.0"   encoding = "utf-8" ?>   
  2. < manifest   xmlns:android = "http://schemas.android .com/apk/res/android "   
  3.       package = "com.ray.test"   
  4.       android :versionCode = "1"   
  5.       android :versionName = "1.0" >   
  6.     < application   android :icon = "@drawable/icon"   android :label = "@string/app_name" >   
  7.         < activity   android :name = ".TestBundle"   
  8.                   android :label = "@string/app_name" >   
  9.             < intent-filter >   
  10.                 < action   android :name = "android .intent.action.MAIN"   />   
  11.                 < category   android :name = "android .intent.category.LAUNCHER"   />   
  12.             </ intent-filter >   
  13.         </ activity >   
  14.           
  15.         < activity   android :name = ".Target" > </ activity >   
  16.     </ application >   
  17.     < uses-sdk   android :minSdkVersion = "3"   />   
  18. </ manifest >    

 

第一个活动的代码如下:

Java代码
  1. package  com.ray.test;  
  2.   
  3. import  android .app.Activity;  
  4. import  android .content.Intent;  
  5. import  android .os.Bundle ;  
  6. import  android .view.MotionEvent;  
  7.   
  8. public   class  TestBundle  extends  Activity {  
  9.     public   void  onCreate(Bundle  savedInstanceState) {  
  10.         super .onCreate(savedInstanceState);  
  11.         setContentView(R.layout.main);  
  12.     }  
  13.       
  14.     public   boolean  onTouchEvent(MotionEvent event) {  
  15.         Intent intent = new  Intent();  
  16.         intent.setClass(TestBundle.this , Target. class );  
  17.         Bundle  mBundle = new  Bundle ();  
  18.         mBundle.putString("Data""ray'blog" ); //压入数据   
  19.         intent.putExtras(mBundle);  
  20.         startActivity(intent);  
  21.         finish();  
  22.         return   super .onTouchEvent(event);  
  23.     }  
  24. }  
package com.ray.test;

import android
.app.Activity;
import android
.content.Intent;
import android
.os.Bundle
;
import android
.view.MotionEvent;

public class TestBundle extends Activity {
    public void onCreate(Bundle
 savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
	public boolean onTouchEvent(MotionEvent event) {
		Intent intent = new Intent();
		intent.setClass(TestBundle.this, Target.class);
		Bundle
 mBundle = new Bundle
();
		mBundle.putString("Data", "ray'blog");//压入数据
		intent.putExtras(mBundle);
		startActivity(intent);
		finish();
		return super.onTouchEvent(event);
	}
}

 

将要跳转到的活动代码:

Java代码
  1. package  com.ray.test;  
  2.   
  3. import  android .app.Activity;  
  4. import  android .os.Bundle ;  
  5.   
  6. public   class  Target  extends  Activity{  
  7.       
  8.     public   void  onCreate(Bundle  savedInstanceState) {  
  9.         super .onCreate(savedInstanceState);  
  10.         setContentView(R.layout.main);  
  11.         Bundle  bundle  = getIntent().getExtras();    
  12.         String data=bundle .getString("Data" ); //读出数据   
  13.         setTitle(data);  
  14.     }  
  15. }  
package com.ray.test;

import android
.app.Activity;
import android
.os.Bundle
;

public class Target extends Activity{
    
	public void onCreate(Bundle
 savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Bundle
 bundle
 = getIntent().getExtras();  
        String data=bundle
.getString("Data");//读出数据
        setTitle(data);
    }
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值