android 里的各种通信方式

这几天 想把 Android里的通信方式 做一些总结。我一步步来吧。从最简单的开始。由于经验有限,有不妥之处,还要多多包涵。


一:intent 传递方式(包括activity之间,activity到service等等)

Activity它们之间就会存在相互转跳的关系      转跳的实现方式还是使用Intent  然后startActivity  ,当然转跳的话是可以带数据过去的。比如从A跳到B 可以把A中的一些数据通过Intent传递给B 。

这里,传递 数据比较简单了。

 

    1.  Intent(mContext,ShowActivity.);     
    2.        
    3. );       
    4.      Bundle bundle =  Bundle();    
    5. );    
    6.      bundle.putInt();       
    7.   
    8.  

          Bundle.putSerializable(Key,Object);  //实现Serializable接口的对象
          Bundle.putParcelable(Key, Object); //实现Parcelable接口的对象
          Bundle bundle = new Bundle();
          bundle.putSerializable("user", user);
          intent.putExtras(bundle);
          this.startActivity(intent);
      
      

      二:Fragment之间的通信方式

      Fragment之间传递参数通信就相对于activity而言,就差多了。但是他们可以 通过他们 宿主 activity来通信,比如要调用某一个Fragment的方法,则想办法从宿主activity去调用另一个Fragment的方法。再不行,还可以通过广播,不建议 直接去调用另一个Fragment的方法。


      三:UI线程和子线程之间的通信

       handler 大家可以把它想象成主线程和子线程的一个通信处理类,它可以给主线程(UI线程)发送数据从而更新主线程(UI线程)的UI与逻辑。

      // 定时器 线程
      class ChangeTextThread implements Runnable{
      @Override
      public void run() {
      synchronized (broadCastTextView) {
      while (true) {
      sCount++;
      if(sCount>=contentList.size()){
      sCount=0;
      }
      try {
      Thread.sleep(3000);  
      } catch (InterruptedException e) {
      e.printStackTrace();
      }
      handler.post(new Runnable() {
      @Override
      public void run() {

      broadCastTextView.next();
        broadCastTextView.setText(contentList.get(sCount));
      }
      });
      }}} }



      四:notifation 通知栏

       Notifation通知栏会在屏幕上方向用户提示信息 但是不会打断用户正在阅读的内容,除非用户手动将 Notifation通知栏拉下。 Notifation的好处就是在于不会影响用户的操作,比如用户正在阅读非常重要的信息这时候帮他直接打开一个activity会非常不合适 因为直接影响到了他当时的操作行为 所以Notifation就出来了。建议大家在开发中遇到可能打断用户使用的情况下都去使用Notifation通知栏。

       

      1. public NotificationActivity  Activity {    
      2.     NotificationManager mManager = ;    
      3. ;    
      4.         
      5.   onCreate(Bundle savedInstanceState) {    
      6.     setContentView(R.layout.notification);    
      7.   
      8.   
      9.  Notification(R.drawable.jay,    
      10.         , System.currentTimeMillis());    
      11.   
      12.   
      13.     Intent intent =  Intent(, MyShowActivity.);    
      14.   
      15.     Bundle bundle =  Bundle();    
      16. );    
      17.     intent.putExtras(bundle);    
      18.   
      19. ,    
      20.         R.string.app_name, intent, PendingIntent.FLAG_UPDATE_CURRENT);    
      21. ,    
      22.           OnClickListener() {    
      23.     
      24.   onClick(View arg0) {    
      25.           
      26. , notification);    
      27.         }    
      28.  OnClickListener() {    
      29.     
      30.   onClick(View arg0) {    
      31.           
      32. .onCreate(savedInstanceState);    
      33.     }    
      34. }    



      五:广播

      Android开发中如果须要对两个完全没关系的程序之间进行通信 就可以使用发送广播与接收广播的机制来实现 ,例如程序A发送了一个广播 程序B接受到 做一些事情 这样就达到了相互的通讯。

       

      copy
       
      public BroadcastActivity  Activity {    
    9.     
    10. ;    
    11.     Button mButton1 = ;    
    12.     
    13.   onCreate(Bundle savedInstanceState) {    
    14.     setContentView(R.layout.broadcast);    
    15.  OnClickListener() {    
    16.             
    17.     
    18.           onClick(View arg0) {    
    19.  Intent(MyService.SEND_OK_MESSAGE);    
    20.                 intent.putExtra();    
    21.  OnClickListener() {    
    22.     
    23.   onClick(View arg0) {    
    24.                 Intent intent =  Intent(MyService.SEND_CANCLE_MESSAGE);    
    25. );    
    26.                 sendBroadcast(intent);    
    27.   
    28.  Intent(, MyService.);    
    29.     startService(i);    
    30. .onCreate(savedInstanceState);    
    31.     }    
    32. }    

    33. copy
       
      接收广播的话 我们开启一个service 在service中通过BroadcastReceiver 来接收广播 前提是须要接收的广播须要在onStart()中注册一下 在AndroidManifest.xml中可以过滤只接收须要接收的广播、  
    34.   
    35. >    
    36. ></action>    
    37.  />    
    38.         <action android:name= />    
    39. public MyService  Service {    
    40.    String SEND_OK_MESSAGE = ;    
    41.    String SEND_CANCLE_MESSAGE = ;    
    42.         
    43.  BroadcastReceiver myBroadCast =  BroadcastReceiver() {    
    44.     
    45.     
    46.       onReceive(Context context, Intent intent) {    
    47.  (action.equals(SEND_OK_MESSAGE)) {    
    48.  + SEND_OK_MESSAGE, Toast.LENGTH_LONG).show();    
    49.         } (action.equals(SEND_CANCLE_MESSAGE)) {    
    50.  + SEND_CANCLE_MESSAGE, Toast.LENGTH_LONG).show();    
    51.         }    
    52.     
    53.       onCreate() {    
    54. .onCreate();    
    55.     }    
    56.     
    57.   onStart(Intent intent,  startId) {    
    58.       
    59.  IntentFilter();    
    60.     myFilter.addAction(SEND_OK_MESSAGE);    
    61. .registerReceiver(myBroadCast, myFilter);    
    62. .onStart(intent, startId);    
    63.     }    
    64.     
    65.      IBinder onBind(Intent arg0) {    
    66.  ;    
    67.     }    
    68.  >    
    69.       <intent-filter>    
    70.  />    
    71.      </intent-filter>    
    72.  />    
    73. public MyBootReceiver  BroadcastReceiver {    
    74.        
    75.   String BOOT_COMPLETED = ;    
    76.     
    77.     
    78.       onReceive(Context context, Intent intent) {    
    79.     
    80.      (intent.getAction().equals(BOOT_COMPLETED)) {    
    81.  Intent(context, MyService.);    
    82.         context.startService(i);    
    83. }   
posted @ 2016-11-18 16:54  天涯海角路  阅读(286)  评论(0)    收藏  举报