我有一个用来调用的服务
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;
}但每当我启动应用程序和关闭应用程序时,它都会崩溃,并导致手机软重启,我对此感到非常困惑,谢谢
发布于 2019-03-09 16:56:52
我的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));
}并不是说这就是解决方案,但它对我来说是有效的。
发布于 2019-03-10 11:22:29
START_NOT_STICKY如果系统在onStartCommand()返回后终止了服务,请不要重新创建服务,除非有挂起的意图要交付。这是最安全的选择,可以避免在不必要的情况下运行您的服务,并且当您的应用程序可以简单地重新启动任何未完成的作业时。
START_STICKY如果系统在onStartCommand()返回后终止了服务,请重新创建服务并调用onStartCommand(),但不要重新传递最后的意图。相反,系统会使用null意图调用onStartCommand(),除非有未完成的意图来启动服务。在这种情况下,这些意图就被传递了。这适用于不执行命令但正在无限期运行并等待作业的媒体播放器(或类似服务)。
START_REDELIVER_INTENT如果系统在onStartCommand()返回后终止了该服务,请重新创建该服务并使用传递给该服务的最后意图调用onStartCommand()。任何挂起的意图都会依次递送。这适用于正在主动执行应立即恢复的作业的服务,例如下载文件。
您可以使用START_NOT_STICKY,但是您必须手动处理服务的停止。还要记住,当您从活动中调用服务时,onCreate()并不总是被调用。只有当您从非活动调用服务时,它才会被调用,否则就会调用onStartCommand()。
我认为这个库有最好的android服务实现。请查看MockGeoFix。
https://stackoverflow.com/questions/55074333
复制相似问题