0

I have a project in which I want to run a task in WorkManager when battery is fully charged. I almost searched everywhere but I couldn't find any solution.

@NonNull
@Override
public Result doWork() {

    //I want to run this task when battery is full.

    if(BatteryManager.BATTERY_STATUS_CHARGING==100) {
          showNotification();
    }

    return Result.success();
}

and the code snippet which starts the WorkManager:

private void startWorkManager() {


    Constraints constraints = new Constraints.Builder()
        .setRequiresBatteryNotLow(true)
        .setRequiresCharging(true)
        .build();

    PeriodicWorkRequest periodicWorkRequest= new
            PeriodicWorkRequest.Builder(NotificationWork.class 
    ,15,TimeUnit.MINUTES)
            .setConstraints(constraints)
            .build();

    WorkManager.getInstance().enqueue(periodicWorkRequest);
}

update the contractor

public NotificationWork(@NonNull Context context, @NonNull WorkerParameters workerParams) { super(context, workerParams); this.context = context;

}

public double batteryLevel(Context context){

    Intent batteryIntent = context.getApplicationContext().registerReceiver(null,
            new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int rawlevel = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    double scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
    double level = -1;
    if (rawlevel >= 0 && scale > 0) {
        level = rawlevel / scale;
    }
    return level;    //This will return the value in range of 0.0-1.0
}


@NonNull
@Override
public Result doWork() {

    if (batteryLevel(context) == 1.0) {  //1.0 means 100%

      showNotification();

        return Result.success();
    } else {
          return Result.retry();
    }
}

1 Answers1

0

First of all, In your worker class, create a batteryLevel(Context context) function to get battery level.

public double batteryLevel(Context context){

    Intent batteryIntent = context.getApplicationContext().registerReceiver(null,
                new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int rawlevel = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    double scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
    double level = -1;
    if (rawlevel >= 0 && scale > 0) {
        level = rawlevel / scale;
    }
    return level;    //This will return the value in range of 0.0-1.0
}

Then in your doWork() method, check for battery level, if it's 100%:

if (batteryLevel(context) == 1.0){  //1.0 means 100%
    //Do your stuff
    return Result.success()
}else{
    return Result.retry()
}

Update Create a class level context object.

Context context;

Initialize your constructor with following parameters

public NotificationWork(@NonNull Context context, @NonNull WorkerParameters workerParams){
    super(context,workParams);
    this.context = context;
}
Umer Farooq
  • 583
  • 3
  • 17
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/198908/discussion-on-answer-by-mysterious-guy-android-workmanager-run-task-when-phone). – Samuel Liew Sep 04 '19 at 00:28