sendBroadcast和sendStickyBroadcast的区别

我们平时最经常使用的是sendBroadcast,就是把一个Intent广播出去。今天我在看wifi的时候,还发现了sendStickyBroadcast。官方文档是这样写的:


public abstract void sendStickyBroadcast (Intent intent)

Since: API Level 1
Perform a sendBroadcast(Intent) that is "sticky," meaning the Intent you are sending stays around after the broadcast is complete, so that others can quickly retrieve that data through the return value of registerReceiver(BroadcastReceiver, IntentFilter). In all other ways, this behaves the same as sendBroadcast(Intent).
You must hold the BROADCAST_STICKY permission in order to use this API. If you do not hold that permission, SecurityException will be thrown.
Parameters


intent The Intent to broadcast; all receivers matching this Intent will receive the broadcast, and the Intent will be held to be re-broadcast to future receivers.

光从字面的意思是很难理解的。只有你写例子才会明白的。

Java代码  收藏代码
  1. package com.android.testbroadcast;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10.   
  11. public class MainActivity extends Activity {  
  12.     Button btnSendi;  
  13.     Button btnSends;  
  14.     Button btnStart;  
  15.     Context mContext;  
  16.     /** Called when the activity is first created. */  
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.main);  
  21.         btnSendi=(Button) findViewById(R.id.sendi);  
  22.         btnSends=(Button) findViewById(R.id.sends);  
  23.         btnStart=(Button) findViewById(R.id.start);  
  24.         mContext=getApplicationContext();  
  25.         btnSendi.setOnClickListener(new OnClickListener(){  
  26.   
  27.             @Override  
  28.             public void onClick(View v) {  
  29.                 // TODO Auto-generated method stub  
  30.                 Intent intent = new Intent();  
  31.                 intent.setAction("com.android.my.action");  
  32.                 intent.setFlags(1);  
  33.                 mContext.sendBroadcast(intent);  
  34.             }  
  35.               
  36.         });  
  37.           
  38.         btnStart.setOnClickListener(new OnClickListener(){  
  39.   
  40.             @Override  
  41.             public void onClick(View v) {  
  42.                 // TODO Auto-generated method stub  
  43.                 Intent intent = new Intent(MainActivity.this,ReceiverActivity.class);  
  44.                  
  45.                 startActivity(intent);  
  46.             }  
  47.               
  48.         });  
  49.           
  50.         btnSends.setOnClickListener(new OnClickListener(){  
  51.   
  52.             @Override  
  53.             public void onClick(View v) {  
  54.                 // TODO Auto-generated method stub  
  55.                 Intent intent = new Intent();  
  56.                 intent.setAction("com.android.my.action.sticky");  
  57.                 intent.setFlags(2);  
  58.                 mContext.sendStickyBroadcast(intent);  
  59.             }  
  60.               
  61.         });  
  62.     }  
  63. }  


Java代码  收藏代码
  1. package com.android.testbroadcast;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.BroadcastReceiver;  
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.content.IntentFilter;  
  8. import android.net.wifi.WifiManager;  
  9. import android.os.Bundle;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13.   
  14. public class ReceiverActivity extends Activity {  
  15.      private IntentFilter mIntentFilter;  
  16.       
  17.     /** Called when the activity is first created. */  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.         mIntentFilter = new IntentFilter();  
  23.         mIntentFilter.addAction("com.android.my.action");  
  24.         mIntentFilter.addAction("com.android.my.action.sticky");  
  25.   
  26.               
  27.     }  
  28.     private BroadcastReceiver mReceiver = new BroadcastReceiver() {  
  29.   
  30.         @Override  
  31.         public void onReceive(Context context, Intent intent) {  
  32.             final String action = intent.getAction();  
  33.             System.out.println("action"+action);  
  34.               
  35.         }  
  36.     };  
  37.       
  38.     @Override  
  39.     protected void onResume() {  
  40.         // TODO Auto-generated method stub  
  41.         super.onResume();  
  42.         registerReceiver(mReceiver, mIntentFilter);  
  43.     }  
  44.       
  45.     @Override  
  46.     protected void onPause() {  
  47.         // TODO Auto-generated method stub  
  48.         super.onPause();  
  49.         unregisterReceiver(mReceiver);  
  50.     }  
  51.       
  52.       
  53. }  



在MainActivity里面会有sendBroadcast和sendStickyBroacat.在ReceverActivity里面通 过BroadcastReceiver来接收这两个消息,在ReceiverActivity里是通过代码来注册Recevier而不是在 Manifest里面注册的。所以通过sendBroadcast中发出的intent在ReceverActivity不处于onResume状态是无 法接受到的,即使后面再次使其处于该状态也无法接受到。而sendStickyBroadcast发出的Intent当ReceverActivity重 新处于onResume状态之后就能重新接受到其Intent.这就是the Intent will be held to be re-broadcast to future receivers这句话的表现。就是说sendStickyBroadcast发出的最后一个Intent会被保留,下次当Recevier处于活跃的 时候,又会接受到它。

posted @ 2011-11-10 15:35  一根骨头棒子*熬的汤  阅读(18723)  评论(1编辑  收藏  举报