简体   繁体   English

如何检测电源连接状态?

[英]How to detect power connected state?

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

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

Set up a BroadcastReceiver for ACTION_BATTERY_CHANGED .ACTION_BATTERY_CHANGED设置一个BroadcastReceiver An Intent extra will tell you what the charging state is -- see BatteryManager for details. Intent extra 将告诉您充电状态是什么——请参阅BatteryManager了解详细信息。

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

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. 1=已连接,0=未连接。 Reflects USB connection status.反映 USB 连接状态。

cat /sys/class/power_supply/ac/online猫 /sys/class/power_supply/ac/online

1=connected, 0=not. 1=已连接,0=未连接。 Reflects AC connection status.反映交流连接状态。

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.文件是 RO,只读,你只会阅读它们来轮询信息。 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. android API 没有提供我需要使用的版本(5.1.1)所需的详细程度,但确实如此。 I used provided android API to create a process run those commands.我使用提供的 android API 来创建一个运行这些命令的进程。 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).您也可以仅使用 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());
}

from Android doc, just do like this来自Android doc,就这样做

        Intent batteryStatus = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

        // Are we charging / charged?
        int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                status == BatteryManager.BATTERY_STATUS_FULL;

        // How are we charging?
        int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
        boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;

One thing more you must check whether there is errors in Manifest--->application.还有一件事你必须检查 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谢谢 Pzycoderz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM