【问题标题】:Android Foreground ServicesAndroid 前台服务
【发布时间】:2019-03-10 11:22:29
【问题描述】:

我有一个被调用的服务

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

但是每当我启动应用程序并关闭应用程序时,它就会崩溃并导致手机软重启,我对此感到非常困惑,谢谢

【问题讨论】:

    标签: android foreground-service


    【解决方案1】:

    我的 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));
        }
    

    并不是说这是解决方案,但它对我有用。

    【讨论】:

      【解决方案2】:
      START_NOT_STICKY
      

      如果系统在 onStartCommand() 返回后终止服务,请不要重新创建服务,除非有待交付的意图。这是最安全的选择,可以避免在不必要的情况下运行您的服务,并且当您的应用程序可以简单地重新启动任何未完成的作业时。

      START_STICKY
      

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

      START_REDELIVER_INTENT
      

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

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

      我认为这个库有最好的 android 服务实现。看看MockGeoFix

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-11
        • 1970-01-01
        • 1970-01-01
        • 2020-06-05
        • 2014-05-25
        • 2023-03-28
        • 2011-09-15
        相关资源
        最近更新 更多