我有一个叫做的服务
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
getActivity().startForegroundService(new Intent(getActivity(),
Background.class));
} else {
getActivity().startService(new Intent(getActivity(), Background.class));
}
以及它自身的服务
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this,"Creating Notification",Toast.LENGTH_SHORT).show();
//
initChannels(this);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, "default")
.setContentTitle("Zeep!?")
.setTicker("Zeep!?")
.setContentText("We're currently working in the background")
.setSmallIcon(R.mipmap.zeep_icon_b)
.setOngoing(true)
.setPriority(Notification.PRIORITY_MIN)
.setContentIntent(pendingIntent)
.build();
startForeground(1337, notification);
//
return START_NOT_STICKY;
}
但每当我启动应用程序并关闭应用程序时,它会崩溃并导致手机软重启,我对此感到困惑,谢谢
我的onStartCommand()看起来像这样:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Tells the system to not try to recreate the service after it has been killed.
return START_NOT_STICKY;
}
我负责处理onCreate()中的通知内容。此外,您需要在调用startForegroundService()后立即调用startForeground():
@Override
public void onCreate() {
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Android O requires a Notification Channel.
if (Build.VERSION.SDK_INT >= 26) {
CharSequence name = getString(R.string.app_name);
// Create the channel for the notification
@SuppressLint("WrongConstant")
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_LOW);
// Set the Notification Channel for the Notification Manager.
if (notificationManager != null) {
notificationManager.createNotificationChannel(mChannel);
}
//Since MainActivity binds with the service and calls onCreate, we can actually call startForegroundService from within the service itself.
startForegroundService(new Intent(ForegroundService.this, ForegroundService.class));
//We only need to call this for SDK 26+, since startForeground always has to be called after startForegroundService.
startForeground(NOTIFICATION_ID, getNotification());
}
else {
//Since MainActivity binds with the service and calls onCreate, we can actually call startService from within the service itself.
startService(new Intent(ForegroundService.this, ForegroundService.class));
}
不是说这是解决方案,但它对我有用。
START_NOT_STICKY
如果系统在onStartCommand()返回后终止服务,则除非有待传递的意图,否则不要重新创建服务。这是最安全的选项,可以在不需要时以及应用程序只需重新启动任何未完成的作业时避免运行服务。
START_STICKY
如果系统在onStartCommand()返回后终止服务,则重新创建服务并调用onStartCommand(),但不重新传递最后一个意图。相反,系统使用null意图调用onStartCommand(),除非有待启动的意图来启动服务。在那种情况下,这些意图被交付。这适用于不执行命令但无限期运行并等待工作的媒体播放器(或类似服务)。
START_REDELIVER_INTENT
如果系统在onStartCommand()返回后终止服务,则重新创建服务并使用传递给服务的最后一个意图调用onStartCommand()。任何待处理的意图依次交付。这适用于主动执行应立即恢复的作业的服务,例如下载文件。
你可以使用START_NOT_STICKY
但是你必须手动处理停止服务。还要记住,当您从活动中调用服务时,onCreate()
并不总是被调用。只有当您从非活动中调用服务时才会调用它,否则将调用onStartCommand()
。
我认为这个库有最好的android服务实现。看看MockGeoFix。