-2

i want to make my app when Battery Be Low Like from 6% to 4% Make a Push a Notification every 1 Minute while app is Close

i was Use background Service and Use BrodcastReceiver and many other things But all tell me Alarm manger will work with my app cause that work in Background

i want app Run on Background when user Download app to make app Listen to battery Level and when battery be 6% to 4% push Notification every 1 minute the Work manager Notification

 public class NotificationWork extends Worker {
private static final String WORK_RESULT = "work_result";

public NotificationWork(@NonNull Context context, @NonNull WorkerParameters workerParams) {
    super(context, workerParams);
}

@NonNull
@Override
public Result doWork() {



    showNotification();


    return Result.success();
}

private void showNotification() {

    NotificationManager manager = (NotificationManager) getApplicationContext().getSystemService(
            Context.NOTIFICATION_SERVICE);

    String channelId = "task_channel";
    String channelName = "task_name";

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        NotificationChannel channel = new
                NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
        manager.createNotificationChannel(channel);
    }

    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), channelId)
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setContentText("text")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
            // Set the intent that will fire when the user taps the notification
            .setAutoCancel(true);


    manager.notify(1, builder.build());
}
 }

and method to start work Manager

private void startWorkManager() {


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

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

    WorkManager.getInstance().enqueue(periodicWorkRequest);


}

1 Answers1

0

Yes, the Alarm manager is not called when the application removes from the background. You can user Work Manager that provides the background process even application removes from the background. But there is one limitation of it. The work manager's minimum time interval is 15 min. so that the work manager calls the worker calls at every 15 mins of interval. Work Manager

I hope, this will help you.

Happy coding...

Pratik Satani
  • 1,115
  • 1
  • 9
  • 25
  • Okay i will work with WorkManager but can i Know how that use and when that Run on Background or Run Forever – Basel Beso Aug 14 '19 at 13:26
  • This will work on the background until the application present in the device. – Pratik Satani Aug 19 '19 at 13:28
  • The app on the device already and all things work good But all things working good just when the App is Open , but Nothing work when app be in Background – Basel Beso Aug 20 '19 at 17:17
  • The work manager is working in the background. What time did you set in the workmanager interval? – Pratik Satani Aug 21 '19 at 06:11
  • i make that Run when Phone battery be Low but this Just work when app is Open – Basel Beso Aug 21 '19 at 07:55
  • The work manager is working when the app is not open. Can you please share your code of work manager so I will suggest solution – Pratik Satani Aug 21 '19 at 12:35
  • i Update that you can Look Now my Work Manager to push a Notification – Basel Beso Aug 21 '19 at 14:36
  • Okay. I got the issue. You have to use PeriodicWorkRequest instead of OneTimeWorkRequest. So it will call every specific time of interval even application is not the background. Check this link: https://developer.android.com/reference/androidx/work/PeriodicWorkRequest – Pratik Satani Aug 22 '19 at 09:18
  • yes i understand that then but now i want app work on background when app is Close Like you say " So it will call every specific time of interval even application is not the background. " i want Just work on Background when app is Closed :) – Basel Beso Aug 22 '19 at 14:31
  • You have to set PeriodicWorkRequest once after that work manager automatically calls a worker at a specific time of interval which you will mention in it. But make sure that the time interval is 15 min or greater. This is working even application is not in the background. – Pratik Satani Aug 23 '19 at 04:57
  • so i am so sorry but can you see me how that PeriodicWorkRequest are work as alternative OneTimeWorkRequest cause really i never use WorkManager Before and i not see how i use that i see many Articles really but Nothing help me – Basel Beso Aug 23 '19 at 16:25
  • PeriodicWorkRequest is working the same as an alarm manager. As per your code, the worker is calling every 15 min. – Pratik Satani Aug 26 '19 at 13:42
  • i do that and that work Good on Background But i have Problem here then i Not want that Work By Time i mean Not want that work Every 15 Min i Just want that will work When the Battery be Full – Basel Beso Aug 27 '19 at 13:33
  • i Update you can see my Code too :) – Basel Beso Aug 27 '19 at 13:34
  • + i have a Problem that App Not Work on Android 4.4 are the Work manage Not Support to Work on Android 4.4 – Basel Beso Aug 27 '19 at 13:55
  • Yes. WorkManger supports above 4.4 – Pratik Satani Aug 28 '19 at 06:51
  • so i want to Know why that work Every 15 Min are i have any way to make that when just batter be Full i mean when Just BatteryManager.BATTERY_STATUS_FULL – Basel Beso Aug 28 '19 at 19:24