android – 检测正在插入的设备

栏目: IOS · Android · 发布时间: 7年前

内容简介:我希望能够检测设备是否已插入.我希望能够以与连接状态相同的方式查询.这是可能的还是我需要创建一个侦听电池事件的广播接收器?翻译自: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


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Algorithms

Algorithms

Sanjoy Dasgupta、Christos H. Papadimitriou、Umesh Vazirani / McGraw-Hill Education / 2006-10-16 / GBP 30.99

This text, extensively class-tested over a decade at UC Berkeley and UC San Diego, explains the fundamentals of algorithms in a story line that makes the material enjoyable and easy to digest. Emphasi......一起来看看 《Algorithms》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码