Get Wireless Charging Information

I am writing an application that registers battery status, but there is a problem with BatteryManager.BATTERY_PLUGGED_WIRELESS . If I download my Galaxy S3 with Android 4.0.4 or even S4 from 4.2.2 to a wireless charging station, the application will display 1 ( BATTERY_PLUGGED_AC ) instead of 4 ( BATTERY_PLUGGED_WIRELESS ). A pop-up window will appear on the phone’s display, indicating wireless charging. So is this an api error?

Can anyone confirm this? Is there any other way around the charging mode?

 public class BatteryReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0); Toast.makeText(context, errorMsg, Toast.LENGTH_SHORT).show(); } } 
+4
source share
1 answer

If I understand correctly, you want to find out if the device is charging or not. I think the solution to the problem should be written like this:

 public class DataReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equalsIgnoreCase(Intent.ACTION_POWER_CONNECTED)) { //do something when device is charging } else if(intent.getAction().equalsIgnoreCase(Intent.ACTION_POWER_DISCONNECTED)) { //do something when device is not charging } } } 

In xml, the receiver will look like this.

  <receiver android:name=".DataReceiver" android:enabled="true"> <intent-filter> <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/> <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/> </intent-filter> </receiver> 

Hope this helps you. :-)

-one
source

Source: https://habr.com/ru/post/1497417/


All Articles