android 系统广播累积

2019-07-14 00:14发布

系统广播积累

ACTION_POWER_CONNECTED                   设备连接电源广播
ACTION_POWER_DISCONNECTED            设备断开电源
ACTION_SCREEN_OFF 锁屏
ACTION_SCREEN_ON 屏幕解锁(再按下设备上的解锁按钮时发送广播的)




添加广播步骤:

    1.定义成员变量。 private BroadcastReceiver mSystemReciver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); if (Intent.ACTION_POWER_CONNECTED.equals(action)){ System.out.println("连上电源"); }else if (Intent.ACTION_POWER_DISCONNECTED.equals(action)){ System.out.println("断开电源"); }else if (Intent.ACTION_SCREEN_ON.equals(action)){ System.out.println("屏幕解锁"); }else if (Intent.ACTION_SCREEN_OFF.equals(action)){ System.out.println("屏幕加锁"); } } };
2.注册广播 private void registerBroadcast(){ IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_POWER_CONNECTED); filter.addAction(Intent.ACTION_POWER_DISCONNECTED); filter.addAction(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_SCREEN_ON); this.registerReceiver(mSystemReciver, filter); }
3.在不使用的时候需要注销广播 unregisterReceiver(mSystemReciver);