如何在 Android 上 运行 无限服务(前台服务已终止)?
How to run infinite service on Android (Foreground service killed)?
我创建了一个粘性前台服务,每次它被杀死时都会重新启动,它似乎工作正常但我注意到在某些情况下(当 OS 杀死它时)它只是不会重新启动。我怎样才能让它在每次被杀死时重新启动?我希望此服务始终 运行。
喜欢那些服务。
这是我的前台服务 onStartCommand:
public int onStartCommand(Intent intent, int flags, int startId) {
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("InsiteMobile Service")
.setContentText("Run the app by clicking here")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent)
.build();
Log.i("InsiteMobileLog", "Service Started");
startForeground(1, notification);
//do heavy work on a background thread
//stopSelf();
return START_STICKY;
}
这就是我在 MainActivity onCreate() 中调用服务的方式:
public void startService() {
Intent serviceIntent = new Intent(this, AppService.class);
serviceIntent.putExtra("inputExtra", "InsiteMobileService");
ContextCompat.startForegroundService(this, serviceIntent);
}
Android Oreo 有一些 limitations about services. You can look this thread . Also Evernote team has good library about background services compatible with Android Oreo (API Level 26). JobScheduler 库使用 JobInfo.Builder.setPeriodic(long intervalMillis, long flexMillis) 方法提供定期服务。该服务的最短工作时间为 15 分钟。部分前台服务教程:
您无法创建始终在最新 OS 版本上运行的服务。但是可以用ForegroundService with notification,那是杀不死的。原则是用户应该始终看到您的应用程序 运行 并消耗 OS 资源。
目前我正在从事同一个项目,我需要在后台提供 运行 服务。有几件事我们需要关心
1. 在服务的onDestroy()方法中,使用startService Intent
再次启动自身
2.For OS 和以下 kitkat 使用 onTaskRemoved(intent); in onStartCommand() 方法
3. onStartCommand()
中的ReturnSTART_STICKY
在此之后,您还需要了解一件事,设备的 phone 管理器 往往会根据 电池杀死后台进程优化 政策。
检查测试设备的 phone 管理器设置,然后重试。
此外,如果您打算使用 notification 使服务保持活动状态,您需要重新检查通过您的应用程序发送通知的权限。
我也在做一个需要不朽服务的项目,所以你应该为此使用Job Scheduler。
我创建了一个粘性前台服务,每次它被杀死时都会重新启动,它似乎工作正常但我注意到在某些情况下(当 OS 杀死它时)它只是不会重新启动。我怎样才能让它在每次被杀死时重新启动?我希望此服务始终 运行。
喜欢那些服务。
这是我的前台服务 onStartCommand:
public int onStartCommand(Intent intent, int flags, int startId) {
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("InsiteMobile Service")
.setContentText("Run the app by clicking here")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent)
.build();
Log.i("InsiteMobileLog", "Service Started");
startForeground(1, notification);
//do heavy work on a background thread
//stopSelf();
return START_STICKY;
}
这就是我在 MainActivity onCreate() 中调用服务的方式:
public void startService() {
Intent serviceIntent = new Intent(this, AppService.class);
serviceIntent.putExtra("inputExtra", "InsiteMobileService");
ContextCompat.startForegroundService(this, serviceIntent);
}
Android Oreo 有一些 limitations about services. You can look this thread
您无法创建始终在最新 OS 版本上运行的服务。但是可以用ForegroundService with notification,那是杀不死的。原则是用户应该始终看到您的应用程序 运行 并消耗 OS 资源。
目前我正在从事同一个项目,我需要在后台提供 运行 服务。有几件事我们需要关心
1. 在服务的onDestroy()方法中,使用startService Intent
再次启动自身
2.For OS 和以下 kitkat 使用 onTaskRemoved(intent); in onStartCommand() 方法
3. onStartCommand()
在此之后,您还需要了解一件事,设备的 phone 管理器 往往会根据 电池杀死后台进程优化 政策。 检查测试设备的 phone 管理器设置,然后重试。
此外,如果您打算使用 notification 使服务保持活动状态,您需要重新检查通过您的应用程序发送通知的权限。
我也在做一个需要不朽服务的项目,所以你应该为此使用Job Scheduler。