Android 前台服务

Android Foreground Services

我有一个服务是用[=​​13=]调用的

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;
}

但是每当我启动应用程序并关闭应用程序时,它就会崩溃并导致 phone 软重启,我对这一切感到很困惑,谢谢

我的 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() returns 之后终止了服务,除非有待交付的意图,否则不要重新创建服务。这是最安全的选择,可以在不必要时避免 运行 您的服务,并且您的应用程序可以简单地重新启动任何未完成的作业。

START_STICKY

如果系统在 onStartCommand() returns 之后终止了服务,请重新创建服务并调用 onStartCommand(),但不要重新传递最后一个 Intent。相反,系统调用 onStartCommand() 时带有一个空意图,除非有未决的意图来启动服务。在那种情况下,这些意图被传递。这适用于不执行命令但 运行 无限期等待作业的媒体播放器(或类似服务)。

START_REDELIVER_INTENT

如果系统在 onStartCommand() returns 之后终止服务,请重新创建服务并使用传递给服务的最后一个意图调用 onStartCommand()。任何未决的意图依次交付。这适用于正在积极执行应立即恢复的工作的服务,例如下载文件。

您可以使用 START_NOT_STICKY,但您必须手动处理服务的停止。 还要记住,当您从 activity 调用服务时,onCreate() 并不总是被调用。只有当您从非 activity 调用服务时,它才会被调用,否则 onStartCommand() 被调用。

我认为该库具有针对 android 的最佳服务实现。检查一下 MockGeoFix