Android 动态广播与静态广播

学而时习,稳固而之心

目前推荐使用动态广播,现在已经很就没有使用过静态广播了.

动态广播与静态广播的联系

1 动态广播的速度要快于静态广播

2 静态广播的生存期可以比动态广播的长很多  

Manifest里面注册的系统广播接收器会被缓存在系统中,即使当App关闭之后,如果有相应的广播发出,应用程序仍然会被唤醒

这也是从8.0以后谷歌显示部分静态广播,推荐使用动态广播

3 动态广播在代码中注册,静态广播在Manifest文件中配置

 

下面 通过2个案例对比 来更好的了解动态广播与静态广播

1 动态广播

activity 里面的代码如下

public class DynamicBroadcast extends AppCompatActivity {
    DyReceive dyReceive;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dynamic_broadcast_layout);

        //实例化IntentFilter对象
        IntentFilter filter = new IntentFilter();
        filter.addAction("huxiaomu");
        dyReceive = new DyReceive();
        //注册广播接收
        registerReceiver(dyReceive, filter);

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("huxiaomu");
                intent.putExtra("type", "胡小牧:发送了动态广播");
                sendBroadcast(intent);

            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(dyReceive);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(dyReceive);
    }
}

自己创建的DyReceive 代码如下

public class DyReceive extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, intent.getStringExtra("type"), Toast.LENGTH_SHORT).show();
    }
}

2 静态广播 

activity 代码如下

public class BroadStaticActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.broad_static_activity_layout);

        findViewById(R.id.st_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("hly");
                intent.setPackage(getPackageName());
                intent.putExtra("type", "胡小牧:发送了静态广播");
                sendBroadcast(intent);
            }
        });

    }
}

Manifest 代码如下

  
            
               
                
            

        

注意:这里enable是指是否启动这个广播接收器;exported:是否允许接收本程序以外的广播

自己创建StReceive 代码如下

public class StReceive extends BroadcastReceiver {

 
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, intent.getStringExtra("type"), Toast.LENGTH_SHORT).show();

    }
}

 

可以看出明显的区别就是一个在代码中注册,一个在配置文件中注册的

 

下面在来一个动态广播监听网络变化的代码如下

public class MainActivity extends AppCompatActivity {
    private static final String tag = "-------------hxm";

    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                ConnectivityManager  connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo info = connectivityManager.getActiveNetworkInfo();
                if (info != null && info.isAvailable()) {
                    String name = info.getTypeName();
                    Log.e(tag, "当前网络名称:" + name);
                    //doSomething()
                } else {
                    Toast.makeText(context, "当前网络不可用", Toast.LENGTH_SHORT).show();
                    Log.e(tag, "没有可用网络");
                    //doSomething()
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        IntentFilter mFilter = new IntentFilter();
        mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(receiver, mFilter);


    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }
}

 

demo 地址参考 希望能更好的理解广播

你可能感兴趣的:(Android,知识点记录)