Android 如何检测电源连接状态?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3267161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:22:51  来源:igfitidea点击:

How to detect power connected state?

android

提问by NPike

Is there an easy way to be notified when USB or AC power is connected to an Android phone?

当 USB 或 AC 电源连接到 Android 手机时,是否有一种简单的方法可以收到通知?

采纳答案by CommonsWare

Set up a BroadcastReceiverfor ACTION_BATTERY_CHANGED. An Intentextra will tell you what the charging state is -- see BatteryManagerfor details.

设置一个BroadcastReceiverfor ACTION_BATTERY_CHANGED。一个Intent额外的会告诉你的充电状态是什么-看BatteryManager的详细信息。

回答by J.J. Kim

In AndroidManifest.xml

在 AndroidManifest.xml 中

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <receiver android:name=".receiver.PlugInControlReceiver">
        <intent-filter>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
            <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
        </intent-filter>
    </receiver>
</application>

In Code

在代码中

public class PlugInControlReceiver extends BroadcastReceiver {
   public void onReceive(Context context , Intent intent) {
       String action = intent.getAction();

       if(action.equals(Intent.ACTION_POWER_CONNECTED)) {
           // Do something when power connected
       }
       else if(action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
           // Do something when power disconnected
       }
   }
}

回答by iman kazemayni

another way is using battery manager. this i usable for api>=21

另一种方法是使用电池管理器。这我可用于 api>=21

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;
    }
}

回答by Jesse Palmer

Here is another way to poll the information:

这是轮询信息的另一种方法:

read the values here:ex.

阅读此处的值:例如。

via android shell:

通过安卓外壳:

cat /sys/class/power_supply/usb/online

1=connected, 0=not. Reflects USB connection status.

1=已连接,0=未连接。反映 USB 连接状态。

cat /sys/class/power_supply/ac/online

cat /sys/class/power_supply/ac/online

1=connected, 0=not. Reflects AC connection status.

1=已连接,0=未连接。反映交流连接状态。

Using both of these together, I think will tell whether the device is receiving power or not. Not sure if the location is the same for all devices. Found the location the same though on android 7+ and 5+, on a Samsung tablet and a RockChip device.

将这两者结合使用,我认为可以判断设备是否正在通电。不确定所有设备的位置是否相同。在 android 7+ 和 5+、三星平板电脑和 RockChip 设备上找到相同的位置。

For the devices I mentioned tested, it worked. Files are RO, read only, you would only read them to poll the information. The android API's did not provide the level of detail I needed at the version I was required to use (5.1.1), this did. I used provided android API to create a process run those commands. It doesn't require root. This was done for an kiosk app. You can also run the same process using only android API (file, FileReader, etc).

对于我提到的测试设备,它有效。文件是 RO,只读,您只能读取它们来轮询信息。android API 没有提供我需要使用的版本 (5.1.1) 所需的详细程度,但确实如此。我使用提供的 android API 来创建一个运行这些命令的进程。它不需要root。这是为自助服务终端应用程序完成的。您还可以仅使用 android API(文件、FileReader 等)运行相同的进程。

Here is an Android API example:

这是一个 Android API 示例:

File aFile = new File("/sys/class/power_supply/ac/online");
try {
    BufferedReader br = new BufferedReader(new FileReader(aFile));
    char aBuff[] = new char[1];
    int aCount = br.read(aBuff,0, 1);
    Log.d(TAG, "run: Res:"+new String(aBuff));
}catch(Exception e){
    Log.d(TAG, "Exception:Error:run: "+e.toString());
}

回答by Girish Sasidharan

One thing more you must check whether there is errors in Manifest--->application. If then click on the field showing error and just click on the link "Name" Then dialogue box appears for adding a class. add the class and in the class copy the code of on receive.That is the code above should be copied in the class file not the main activity. Thanks Pzycoderz

还有一件事你必须检查Manifest--->application 中是否有错误。如果然后单击显示错误的字段,然后单击链接“名称”,则会出现用于添加类的对话框。添加类并在类中复制接收时的代码。即上面的代码应该复制到类文件中而不是主要活动中。感谢 Pzycoderz