zoukankan      html  css  js  c++  java
  • 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.

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

    package com.android.testbroadcast;
     
    
    import android.app.Activity;
     
    import android.content.Context;
     
    import android.content.Intent;
     
    import android.os.Bundle;
     
    import android.view.View;
     
    import android.view.View.OnClickListener;
     
    import android.widget.Button;
     
    
    public class MainActivity extends Activity {
     
            Button btnSendi;
     
            Button btnSends;
     
            Button btnStart;
     
            Context mContext;
     
        /** Called when the activity is first created. */
     
        @Override
     
        public void onCreate(Bundle savedInstanceState) {
     
            super.onCreate(savedInstanceState);
     
            setContentView(R.layout.main);
     
            btnSendi=(Button) findViewById(R.id.sendi);
     
            btnSends=(Button) findViewById(R.id.sends);
     
            btnStart=(Button) findViewById(R.id.start);
     
            mContext=getApplicationContext();
     
            btnSendi.setOnClickListener(new OnClickListener(){
     
    
                            @Override
     
                            public void onClick(View v) {
     
                                    // TODO Auto-generated method stub
     
                                    Intent intent = new Intent();
     
                                intent.setAction("com.android.my.action");
     
                                intent.setFlags(1);
     
                                mContext.sendBroadcast(intent);
     
                            }
     
                    
            });
     
            
     
            btnStart.setOnClickListener(new OnClickListener(){
     
    
                            @Override
     
                            public void onClick(View v) {
     
                                    // TODO Auto-generated method stub
     
                                    Intent intent = new Intent(MainActivity.this,ReceiverActivity.class);
     
                               
                                startActivity(intent);
     
                            }
     
                    
            });
     
            
     
            btnSends.setOnClickListener(new OnClickListener(){
     
    
                            @Override
     
                            public void onClick(View v) {
     
                                    // TODO Auto-generated method stub
     
                                    Intent intent = new Intent();
     
                                intent.setAction("com.android.my.action.sticky");
     
                                intent.setFlags(2);
     
                                mContext.sendStickyBroadcast(intent);
     
                            }
     
                    
            });
     
        }
     
    }


     

    package com.android.testbroadcast;
     

    import android.app.Activity;
     
    import android.content.BroadcastReceiver;
     
    import android.content.Context;
     
    import android.content.Intent;
     
    import android.content.IntentFilter;
     
    import android.net.wifi.WifiManager;
     
    import android.os.Bundle;
     
    import android.view.View;
     
    import android.view.View.OnClickListener;
     
    import android.widget.Button;
     

    public class ReceiverActivity extends Activity {
     
             private IntentFilter mIntentFilter;
     
           
        /** Called when the activity is first created. */
     
        @Override
     
        public void onCreate(Bundle savedInstanceState) {
     
            super.onCreate(savedInstanceState);
     
            setContentView(R.layout.main);
     
            mIntentFilter = new IntentFilter();
     
            mIntentFilter.addAction("com.android.my.action");
     
            mIntentFilter.addAction("com.android.my.action.sticky");
     

                           
        }
     
        private BroadcastReceiver mReceiver = new BroadcastReceiver() {
     

            @Override
     
            public void onReceive(Context context, Intent intent) {
     
                final String action = intent.getAction();
     
                System.out.println("action"+action);
     
               
     
            }
     
        };
     
       
        @Override
     
        protected void onResume() {
     
                // TODO Auto-generated method stub
     
                super.onResume();
     
                registerReceiver(mReceiver, mIntentFilter);
     
        }
     
       
        @Override
     
        protected void onPause() {
     
                // TODO Auto-generated method stub
     
                super.onPause();
     
                unregisterReceiver(mReceiver);
     
        }
     
       
       
    }

     /**
    * @author 张兴业
    * 邮箱:xy-zhang#163.com
    * android开发进阶群:278401545
    *
    */

  • 相关阅读:
    线段树优化dp——牛客多校第一场I(好题)
    字符串dp——牛客多校第五场G
    凑出和相等的k组数,玄学结论——hdu6616
    主席树/线段树模拟归并排序+二分答案(好题)——hdu多校第4场08
    思维题+贪心——牛客多校第一场C
    线性基算贡献——19牛客多校第一场H
    俞敏洪语录
    睡眠长短决定寿命!人每天应该睡多少小时
    Java程序员从笨鸟到菜鸟之(三十一)大话设计模式(一)设计模式遵循的七大原则
    spring源码剖析(五)利用AOP实现自定义Spring注解
  • 原文地址:https://www.cnblogs.com/xyzlmn/p/3168104.html
Copyright © 2011-2022 走看看