安卓:检查装置插入 [英] Android: Check if device is plugged in

查看:113
本文介绍了安卓:检查装置插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有一个广播接收器来监听更改ACTION_POWER_CONNECTED,反过来标志在屏幕上停留。

My app has a broadcast receiver to listen for changes to ACTION_POWER_CONNECTED, and in turn flag the screen to stay on.

我所缺少的是检查它第一次运行时的充电状态的能力的应用程序。任何人都可以请帮我用code手动检查充电状态?

What I am missing is the ability for the app to check the charging status when it first runs. Can anyone please help me with code to manually check charging status?

推荐答案

由于这里CommonsWare是code我写的。

Thanks to CommonsWare here is the code I wrote.

public class PowerUtil {
    public static boolean isConnected(Context context) {
        Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
    }
}

这篇关于安卓:检查装置插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆