0

I am new in android. I want to make an app with a service. The service should continuously check the current charging state(Plugged or not). And I want to perform an event(Turn wifi off) when the charger is connected. Can you please solve my problem?

Mohit Tyagi
  • 2,788
  • 4
  • 17
  • 29
Farshan
  • 21
  • 1
  • 3

2 Answers2

1

If you call the function below it can show if the device is charging. You can use that to perform the tasks you need: (This will work for both wired and wireless charging)

public static boolean isPlugged(Context context) {
boolean isPlugged= false;
Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
isPlugged = plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
if (VERSION.SDK_INT > VERSION_CODES.JELLY_BEAN) {
    isPlugged = isPlugged || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
}
return isPlugged;
}

For more information visit the Android Dev site here.

0

Regarding the battery status you can find an examples below:

You can check battery status on-demand:

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


int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                 status == BatteryManager.BATTERY_STATUS_FULL;


int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;`

Or by using BroadcastReceiver:

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

The Code:

public class PowerConnectionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                        status == BatteryManager.BATTERY_STATUS_FULL;
    int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
    boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
}
}

In order to turn WIFI programmatic -

You should define the permissions:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>

To set it On:

 WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
 wifi.setWifiEnabled(true);

To Set it Off:

 WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
 wifi.setWifiEnabled(false);
  • But how can i check it repeatedly in background. – Farshan Sep 24 '17 at 16:22
  • Hi, I believe that you have all info, you can do it repeatedly by taken the information from the broadcast receiver for example by checking the usbCharge status, or make a function that return the usbCharge at on-demand example . – Android and Security Guru Sep 25 '17 at 16:05