Using alarmmanager, Service is not being restarted after application being killed?

时光毁灭记忆、已成空白 提交于 2020-01-16 18:21:09

问题


I have made a simple example where a toast message will be shown after a time gap. I am using alarmmanager for scheduling a service from where the toast message will be shown. Now the problem is when the app is running it works properly, but when I close the app from task-manager the service is not getting restarted. Below is my service code

public class AlarmService extends Service {

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this, "Alarm Alarm Alarm", Toast.LENGTH_SHORT).show();
    return Service.START_STICKY;
}

}

and below is my alarmmanager code :

alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, AlarmService.class);
    pendingIntent = PendingIntent.getService(this, 0, intent, 0);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 10, pendingIntent);

Now, what is the solution for this? I need to keep the alarm even I close the app.


回答1:


You can actually implement BroadcastReceiver at onDestroy to restart the service once the app/service is close by the system/user.

You can check the post here, and a tutorial for you, check here.

Hope it helps!




回答2:


Try with below :-

return Service.START_CONTINUATION_MASK;



来源:https://stackoverflow.com/questions/51798439/using-alarmmanager-service-is-not-being-restarted-after-application-being-kille

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!