Skip to content Skip to sidebar Skip to footer

Cannot Do Work In Background Periodically With Alarmmanager

I want to call a service in background by the hour. first problem is alarm manager is not working smoothly. Timer is terrible, sometimes early sometimes later. second problem is,

Solution 1:

AlarmManager will be works normally on api 27 (oreo 8.1) and less. Just setup with something like that:

publicvoidsetupAlarm(Context context, long triggerAtMillis) {
    PendingIntentpendingIntent= getPendingIntent(context);
    AlarmManageralarmManager= (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        alarmManager.set(AlarmManager.RTC_WAKEUP, triggerAtMillis, AlarmIntentBuilder.buildPendingIntent(context, uri));
    } elseif (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerAtMillis, AlarmIntentBuilder.buildPendingIntent(context, uri));
    } else {
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerAtMillis, AlarmIntentBuilder.buildPendingIntent(context, uri));
    }
}

Don't forget that you need to setup your alarm again every time when your AlarmReceiver triggered. But that code will not be working on api 28 and more (pie 9.0). This is happening because of Power management feature introduced in Android Pie. Strict restrictions are introduced on the apps running in background. These restrictions are explained here. Try to use JobScheculer to invoke your foreground IntentService.

As about your second question. You need to make a notification in your CallService. Just add this onCreate to your IntentService:

@OverridepublicvoidonCreate() {
    super.onCreate();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannelchan=newNotificationChannel("test", "your_channel_name", NotificationManager.IMPORTANCE_NONE);
        NotificationManagermanager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (manager != null) {
            manager.createNotificationChannel(chan);
        }

        Notification.BuildernotificationBuilder=newNotification.Builder(context, "test");
        notificationBuilder.setOngoing(true);
        notificationBuilder.setContentTitle("Updatig")
                .setContentText("Wait for finish updating")
        startForeground(NOTIFICATION_ID, notificationBuilder.build());
    }
}

RemoteServiceException will be thrown if you will not call startForeground in service (you have 5 seconds to make this call) after startForegroundService.

Solution 2:

I had same problem with alarm manager. You can switch to Interval of RxJava.

protectedfunstartCountdown(period: Long, timeUnit: TimeUnit): Observable<Long> {
        return Observable.interval(period, timeUnit)
            .subscribeOn(Schedulers.io())
            .observeOn(Schedulers.io())
    }

usage;

startCountdown(MEDIA_LOCATION_CHANGE_PERIOD, TimeUnit.MILLISECONDS)
        .subscribe {
            fireSomethingUp()
        }

Post a Comment for "Cannot Do Work In Background Periodically With Alarmmanager"