在Android中使用JobScheduler启动服务而不加载UI

问题描述 投票:0回答:1

我已经构建了一个将一些数据发送到服务器的应用程序。我正在使用Job Scheduler服务,该服务启动该应用程序所需的服务。当应用程序位于前台或后台时,它可以正常工作。但是一旦应用程序被杀死,JobScheduler可以正常工作,但是无法启动作为应用程序基础的服务。当应用程序被操作系统杀死后,是否有任何特定的方法来启动服务?任何建议,将不胜感激。

我正在使用Android O并为Job Scheduler类扩展JobService!

android android-studio android-service android-jobscheduler android-background
1个回答
0
投票

当应用具有以下功能时,是否有任何特定的方法来启动服务被操作系统杀死了?

如果服务未运行,您可以使用警报来重新启动它。

您可以执行以下操作:

Calendar now = new GregorianCalendar();
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, now.getTimeInMillis(), 10 * 1000, getMyPendingIntent());

还有待处理的意图:

private PendingIntent getMyPendingIntent() {
    Intent intent = new Intent(getApplicationContext(), MyService.class);
    return PendingIntent.getService(getApplicationContext(), PENDING_REQUEST_CODE, intent, 0);
}
© www.soinside.com 2019 - 2024. All rights reserved.