赞
踩
先注册
mySystemReceiver = new MySystemReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_BATTERY_LOW);
intentFilter.addAction(Intent.ACTION_BATTERY_OKAY);
intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
intentFilter.addAction(Intent.ACTION_SCREEN_ON);
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(mySystemReceiver,intentFilter);
在广播接收者中判断
public class MySystemReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)){ int level = intent.getIntExtra("level", 0); Toast.makeText(context, "当前电量"+level, Toast.LENGTH_SHORT).show(); }else if(intent.getAction().equals(Intent.ACTION_BATTERY_LOW)){ Toast.makeText(context, "电量低", Toast.LENGTH_SHORT).show(); }else if(intent.getAction().equals(Intent.ACTION_BATTERY_OKAY)){ Toast.makeText(context, "电量充足", Toast.LENGTH_SHORT).show(); }else if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){ Toast.makeText(context, "屏幕亮了", Toast.LENGTH_SHORT).show(); }else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){ Toast.makeText(context, "屏幕锁了", Toast.LENGTH_SHORT).show(); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。