• 【转】利用Bundle实现Android Activity间消息的传递


    地址:http://www.iteye.com/topic/424491

    利用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. <activity android:name=".Target"></activity>
    15. </application>
    16. <uses-sdk android:minSdkVersion="3" />
    17. </manifest>

    第一个活动的代码如下:

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

    将要跳转到的活动代码:

    Java代码 复制代码 收藏代码
    1. package com.ray.test;
    2. import android.app.Activity;
    3. import android.os.Bundle;
    4. public class Target extends Activity{
    5. public void onCreate(Bundle savedInstanceState) {
    6. super.onCreate(savedInstanceState);
    7. setContentView(R.layout.main);
    8. Bundle bundle = getIntent().getExtras();
    9. String data=bundle.getString("Data");//读出数据
    10. setTitle(data);
    11. }
    12. }
  • 相关阅读:
    hdu5358 推公式+在一个区间内的尺取+枚举法
    poj3349 散列表(hash)
    hdu3282 链表或者对顶堆
    hdu5178 尺取
    hdu5672 尺取
    hdu3244完全背包+二分答案 留坑
    hdu5256 二分求LIS+思维
    hdu5646数学构造+二分
    hdu4190 二分答案
    Python中Scapy网络嗅探模块的使用
  • 原文地址:https://www.cnblogs.com/cappuccino/p/2112976.html
Copyright © 2020-2023  润新知