sendBroadcast和sendStickyBroadcast的区别

  • Post author:
  • Post category:其他



http://www.cnblogs.com/hoji-real/articles/2244538.html

我们平时最经常使用的是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. import

    android.app.Activity;

  3. import

    android.content.Context;

  4. import

    android.content.Intent;

  5. import

    android.os.Bundle;

  6. import

    android.view.View;

  7. import

    android.view.View.OnClickListener;

  8. import

    android.widget.Button;

  9. public


    class

    MainActivity

    extends

    Activity {
  10. Button btnSendi;
  11. Button btnSends;
  12. Button btnStart;
  13. Context mContext;

  14. /** Called when the activity is first created. */

  15. @Override

  16. public


    void

    onCreate(Bundle savedInstanceState) {

  17. super

    .onCreate(savedInstanceState);
  18. setContentView(R.layout.main);
  19. btnSendi=(Button) findViewById(R.id.sendi);
  20. btnSends=(Button) findViewById(R.id.sends);
  21. btnStart=(Button) findViewById(R.id.start);
  22. mContext=getApplicationContext();
  23. btnSendi.setOnClickListener(

    new

    OnClickListener(){

  24. @Override

  25. public


    void

    onClick(View v) {

  26. // TODO Auto-generated method stub
  27. Intent intent =

    new

    Intent();
  28. intent.setAction(

    “com.android.my.action”

    );
  29. intent.setFlags(

    1

    );
  30. mContext.sendBroadcast(intent);
  31. }
  32. });
  33. btnStart.setOnClickListener(

    new

    OnClickListener(){

  34. @Override

  35. public


    void

    onClick(View v) {

  36. // TODO Auto-generated method stub
  37. Intent intent =

    new

    Intent(MainActivity.

    this

    ,ReceiverActivity.

    class

    );
  38. startActivity(intent);
  39. }
  40. });
  41. btnSends.setOnClickListener(

    new

    OnClickListener(){

  42. @Override

  43. public


    void

    onClick(View v) {

  44. // TODO Auto-generated method stub
  45. Intent intent =

    new

    Intent();
  46. intent.setAction(

    “com.android.my.action.sticky”

    );
  47. intent.setFlags(

    2

    );
  48. mContext.sendStickyBroadcast(intent);
  49. }
  50. });
  51. }
  52. }

Java代码

收藏代码


  1. package

    com.android.testbroadcast;

  2. import

    android.app.Activity;

  3. import

    android.content.BroadcastReceiver;

  4. import

    android.content.Context;

  5. import

    android.content.Intent;

  6. import

    android.content.IntentFilter;

  7. import

    android.net.wifi.WifiManager;

  8. import

    android.os.Bundle;

  9. import

    android.view.View;

  10. import

    android.view.View.OnClickListener;

  11. import

    android.widget.Button;

  12. public


    class

    ReceiverActivity

    extends

    Activity {

  13. private

    IntentFilter mIntentFilter;

  14. /** Called when the activity is first created. */

  15. @Override

  16. public


    void

    onCreate(Bundle savedInstanceState) {

  17. super

    .onCreate(savedInstanceState);
  18. setContentView(R.layout.main);
  19. mIntentFilter =

    new

    IntentFilter();
  20. mIntentFilter.addAction(

    “com.android.my.action”

    );
  21. mIntentFilter.addAction(

    “com.android.my.action.sticky”

    );
  22. }

  23. private

    BroadcastReceiver mReceiver =

    new

    BroadcastReceiver() {

  24. @Override

  25. public


    void

    onReceive(Context context, Intent intent) {

  26. final

    String action = intent.getAction();
  27. System.out.println(

    “action”

    +action);
  28. }
  29. };

  30. @Override

  31. protected


    void

    onResume() {

  32. // TODO Auto-generated method stub

  33. super

    .onResume();
  34. registerReceiver(mReceiver, mIntentFilter);
  35. }

  36. @Override

  37. protected


    void

    onPause() {

  38. // TODO Auto-generated method stub

  39. super

    .onPause();
  40. unregisterReceiver(mReceiver);
  41. }
  42. }

在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处于活跃的 时候,又会接受到它。