内容简介:我希望能够检测设备是否已插入.我希望能够以与连接状态相同的方式查询.这是可能的还是我需要创建一个侦听电池事件的广播接收器?翻译自:https://stackoverflow.com/questions/6217692/detecting-the-device-being-plugged-in
我希望能够检测设备是否已插入.我希望能够以与连接状态相同的方式查询.这是可能的还是我需要创建一个侦听电池事件的广播接收器?
显然, ACTION_BATTERY_CHANGED
是一个“粘性广播”,这意味着你可以在广播后随时注册并接收它.要获得插入状态,您可以执行以下操作:
public void onCreate() { BroadcastReceiver receiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); if (plugged == BatteryManager.BATTERY_PLUGGED_AC) { // on AC power } else if (plugged == BatteryManager.BATTERY_PLUGGED_USB) { // on USB power } else if (plugged == 0) { // on battery power } else { // intent didnt include extra info } } }; IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); registerReceiver(receiver, filter); }
翻译自:https://stackoverflow.com/questions/6217692/detecting-the-device-being-plugged-in
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- HashMap为何从头插入改为尾插入
- C++拾趣——STL容器的插入、删除、遍历和查找操作性能对比(Windows VirtualStudio)——插入
- HashMap之元素插入
- 插入排序
- PHP 实现插入排序
- 特殊排序——二分+插入排序
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Data Structures and Algorithm Analysis in Java
Mark A. Weiss / Pearson / 2011-11-18 / GBP 129.99
Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course wa......一起来看看 《Data Structures and Algorithm Analysis in Java》 这本书的介绍吧!