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