zoukankan      html  css  js  c++  java
  • 使用Handler进行Activity之间的通信

    假设你已经明白下面的内容,那么这篇博客很适合您!

    <1> Application 的作用及用法

    <2> Activity、Task 以及 Application 之间的关系

    <3> Handler 的用法

    真的很感谢 anhenzhufeng 这位 CSDN 好友,如果不是他的虚心和认真,恐怕这篇文章难以问世!

    再次感谢他在我的博客http://blog.csdn.net/androidbluetooth/article/details/6384641#reply的提问,这篇文章送给他以及有需要的朋友们。希望这篇博客能够帮到您!

    读这篇博客之前,我们看看 anhenzhufeng 的问题,见截图,如下:


    大致说一下我的思路吧!

    多个 Activity 之间可以通过 Application 共享数据,在这里我就让两个 Activity 共享 Handler(更新UI,我一般使用 Handler),主 Activity 中更新 UI,另一个 Activity 发送更新UI的消息。这样就达到在主Activity更新UI的目的。好吧,具体看代码!

    1. 主 Activity 的 main.xml

     

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:orientation="vertical"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="fill_parent"  
    6.     >  
    7.     <TextView  
    8.         android:id="@+id/tv"    
    9.         android:layout_width="fill_parent"   
    10.         android:layout_height="wrap_content"   
    11.         android:text="changed before: This is MasterActivity!"  
    12.         />  
    13.     <Button   
    14.         android:layout_marginTop="15dip"  
    15.         android:id="@+id/btn_to"    
    16.         android:layout_width="fill_parent"   
    17.         android:layout_height="wrap_content"   
    18.         android:text="To OtherActivity"/>  
    19.       
    20. </LinearLayout>  

    2. 主 Activity 的Java 代码

     

     

    1. package mark.zhang;  
    2.   
    3. import android.app.Activity;  
    4. import android.content.Intent;  
    5. import android.graphics.Color;  
    6. import android.os.Bundle;  
    7. import android.os.Handler;  
    8. import android.os.Message;  
    9. import android.view.View;  
    10. import android.view.View.OnClickListener;  
    11. import android.widget.Button;  
    12. import android.widget.TextView;  
    13.   
    14. public class MasterActivity extends Activity {  
    15.     // 用于msg.what值  
    16.     private static final int CHANGED = 0x0010;  
    17.       
    18.     private Button btn_to = null;  
    19.     private TextView tv = null;  
    20.       
    21.     private MyHandler handler = null;  
    22.       
    23.     private MyAPP mAPP = null;  
    24.       
    25.   
    26.     @Override  
    27.     public void onCreate(Bundle savedInstanceState) {  
    28.         super.onCreate(savedInstanceState);  
    29.         setContentView(R.layout.main);  
    30.           
    31.         mAPP = (MyAPP) getApplication();  
    32.         handler = new MyHandler();  
    33.           
    34.         tv = (TextView) findViewById(R.id.tv);  
    35.         btn_to = (Button) findViewById(R.id.btn_to);  
    36.         // 设置监听器  
    37.         btn_to.setOnClickListener(new OnClickListener() {  
    38.   
    39.             @Override  
    40.             public void onClick(View v) {  
    41.                 // 设置共享变量  
    42.                 mAPP.setHandler(handler);  
    43.                 // 启动另一个Activity  
    44.                 Intent intent = new Intent(MasterActivity.this,  
    45.                         ToChangeViewActivity.class);  
    46.                 startActivity(intent);  
    47.             }  
    48.         });  
    49.     }  
    50.       
    51.     /** 
    52.      * 自己实现 Handler 处理消息更新UI 
    53.      *  
    54.      * @author mark 
    55.      */  
    56.     final class MyHandler extends Handler {  
    57.         @Override  
    58.         public void handleMessage(Message msg) {  
    59.             super.handleMessage(msg);  
    60.             if(msg.what == CHANGED) { // 更新UI  
    61.                 tv.setText("changed after: I have be changed by Other Activity!");  
    62.                 tv.setBackgroundColor(Color.BLUE);  
    63.                   
    64.                 btn_to.setText("I have been changed!");  
    65.                 btn_to.setBackgroundColor(Color.RED);  
    66.             }  
    67.         }  
    68.     }  
    69. }  

     

    3. 自实现Application

    对于Application可以参考sdk api文档。在这里,我就直接使用,不做解释!

     

    1. package mark.zhang;  
    2.   
    3. import mark.zhang.MasterActivity.MyHandler;  
    4. import android.app.Application;  
    5.   
    6. /** 
    7.  * 自己实现Application,实现数据共享 
    8.  *  
    9.  * @author mark 
    10.  * 
    11.  */  
    12. public class MyAPP extends Application {  
    13.     // 共享变量  
    14.     private MyHandler handler = null;  
    15.       
    16.     // set方法  
    17.     public void setHandler(MyHandler handler) {  
    18.         this.handler = handler;  
    19.     }  
    20.       
    21.     // get方法  
    22.     public MyHandler getHandler() {  
    23.         return handler;  
    24.     }  
    25. }  

    4. 改变主Activity UI 的Activity

     

    该 Activity 是 ToChangeViewActivity,Java、以及布局文件 show.xml 代码如下。

     

    1. package mark.zhang;  
    2.   
    3. import mark.zhang.MasterActivity.MyHandler;  
    4. import android.app.Activity;  
    5. import android.os.Bundle;  
    6. import android.view.View;  
    7. import android.view.View.OnClickListener;  
    8.   
    9. public class ToChangeViewActivity extends Activity {  
    10.     private static final int CHANGED = 0x0010;  
    11.       
    12.     private MyAPP mAPP = null;  
    13.       
    14.     private MyHandler mHandler = null;  
    15.       
    16.     @Override  
    17.     protected void onCreate(Bundle savedInstanceState) {  
    18.         super.onCreate(savedInstanceState);  
    19.         setContentView(R.layout.show);  
    20.           
    21.         mAPP = (MyAPP) getApplication();  
    22.         // 获得该共享变量实例  
    23.         mHandler = mAPP.getHandler();  
    24.           
    25.         findViewById(R.id.btn_chang).setOnClickListener(new OnClickListener() {  
    26.               
    27.             @Override  
    28.             public void onClick(View v) {  
    29.                 // 发送消息  
    30.                 mHandler.sendEmptyMessage(CHANGED);  
    31.                 ToChangeViewActivity.this.finish();  
    32.             }  
    33.         });  
    34.     }  
    35. }  
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout  
    3.   xmlns:android="http://schemas.android.com/apk/res/android"  
    4.   android:orientation="vertical"  
    5.   android:layout_width="fill_parent"  
    6.   android:layout_height="fill_parent"  
    7.   >  
    8.     <TextView  
    9.         android:id="@+id/tv"    
    10.         android:layout_width="fill_parent"   
    11.         android:layout_height="wrap_content"   
    12.         android:text="hello,MasterActivity!"  
    13.         />   
    14.       
    15.     <Button  
    16.         android:id="@+id/btn_chang"    
    17.         android:layout_width="fill_parent"   
    18.         android:layout_height="wrap_content"   
    19.         android:text="change the MasterActivityView..."  
    20.         />   
    21.       
    22. </LinearLayout>  

    5. 修改manifest.xml文件
    这里主要注意两点:

     

    <1> 声明 Application

    <2> 注册 ToChangeViewActivity

    代码,如下:

     

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

    6. 运行效果

     

    点击 " To OtherActivity",进入 ToChangeViewActivity 


    再点击“ change the MasterActivityView...”


    改变效果


    7. 最后思考

    这里只是两个Activity之间交互,多个 Activity 之间需要考虑设置 launchMode 即 Activity 的加载模式,更多关于这方面的知识可以参考:

    http://blog.csdn.net/androidbluetooth/article/details/6547670

    http://download.csdn.net/source/3368975

  • 相关阅读:
    38
    37
    学记
    36.java_exception_test
    c++中enum的用法——枚举类型
    35
    34
    33
    32
    31
  • 原文地址:https://www.cnblogs.com/vegetate/p/9997351.html
Copyright © 2011-2022 走看看