BroadCast

网络状态监听

intentFilter=new IntentFilter();
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        networkChangeReceiver=new NetworkChangeRecevier();
        registerReceiver(networkChangeReceiver,intentFilter);
class NetworkChangeRecevier extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager connectivityManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
            if (networkInfo!=null&&networkInfo.isAvailable()){
                Toast.makeText(context,"network is available",Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(context,"network is unavailable",Toast.LENGTH_SHORT).show();
            }
        }
    }
权限
  

开机广播监听

public class BootCompleteReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"Boot complete",Toast.LENGTH_LONG).show();
    }
}
权限


            
                
            
        

发送广播

发送标准广播

定义发送广播
intent-filter>
                
意图发送
ntent intent=new Intent("text.broadcast.MY_BROAD");
                sendBroadcast(intent);
定义一个类接受广播继承BroadCastReceiver

你可能感兴趣的:(BroadCast)