【问题标题】:I am trying to start Foreground service on older API version. Work on APIs 26+我正在尝试在较旧的 API 版本上启动前台服务。使用 API 26+
【发布时间】:2019-09-06 11:24:27
【问题描述】:

我需要启动我的应用服务前台。我的代码在 API 级别 26 和更高级别上运行良好,但不适用于较旧的 API 级别。在旧版本上,服务显示在正在运行的服务中,但不发送启动通知。我需要对我的代码进行哪些更改,为什么不起作用?

public void onCreate() {
        super.onCreate();
        messageIntent.setAction(getString(R.string.receiver_receive));



        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID_DEFAULT)
                .setOngoing(false).setSmallIcon(R.drawable.ic_launcher).setPriority(Notification.PRIORITY_MIN);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID_DEFAULT,
                    NOTIFICATION_CHANNEL_ID_DEFAULT, NotificationManager.IMPORTANCE_LOW);
            notificationChannel.setDescription(NOTIFICATION_CHANNEL_ID_DEFAULT);
            notificationChannel.setSound(null, null);
            notificationManager.createNotificationChannel(notificationChannel);
            startForeground(1, builder.build());
        }

    }

启动服务

protected void onStart() {
        super.onStart();
        // Bind to LocalService
        Intent intent = new Intent(this, SocketService.class);
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            ContextCompat.startForegroundService(this, new Intent(this, SocketService.class));
        else
            this.startService(new Intent(this, SocketService.class));
    }

【问题讨论】:

  • 你如何开始这个Service
  • @JeelVankhede protected void onStart() { super.onStart(); // Bind to LocalService Intent intent = new Intent(this, SocketService.class); bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) ContextCompat.startForegroundService(this, new Intent(this, SocketService.class)); else this.startService(new Intent(this, SocketService.class)); }
  • startForeground(1, builder.build()); 将此行放在onCreate() 中的 if 条件之外。

标签: java android notifications foreground foreground-service


【解决方案1】:

在 else 语句中为以下 api 启动服务。

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    // only for newer versions
      Intent pushIntent = new Intent(this, ClassName.class);
      startForegroundService(pushIntent);
} else {
      Intent pushIntent = new Intent(this, ClassName.class);
      startService(pushIntent);
}

【讨论】:

  • 这没关系,我的代码与我的代码类似,但是我没有收到启动通知,并且当我关闭任务时我的应用程序无法运行。我认为这与通知代码有关,我更新了问题。 PS:服务显示在运行服务中
  • 您是否尝试从 if 块中取出通知代码?
【解决方案2】:

方法ContextCompat.startForegroundService(...)已经包含if块:

public static void startForegroundService(@NonNull Context context, @NonNull Intent intent) {
    if (Build.VERSION.SDK_INT >= 26) {
        context.startForegroundService(intent);
    } else {
        // Pre-O behavior.
        context.startService(intent);
    }
}

只需致电ContextCompat.startForegroundService(this, pushIntent)

你需要将startForeground移到if块之外:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    ...        
}
startForeground(1, builder.build());

另外,不要忘记在旧 API 上调用 notificationManagerCompat.notify(NOTIFICATION_ID, notification)(如果通知未自动显示)。 NOTIFICATION_ID 不能为零!!!

【讨论】:

    【解决方案3】:

    为api低于26的设备编写代码与if语句中编写的代码相同,但不包括通知通道部分,因为版本低于26的设备不支持通知通道

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // you had written code for device version greater than O
        } else {
           //You need to write code for device with lower version here
        }
    

    【讨论】:

    • 我将该代码放在 else 语句中,现在我有开始通知但无法正常工作,我没有收到其他通知或消息。 else { NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "5"); builder.setSmallIcon(R.drawable.ic_launcher); builder.setContentTitle(title); builder.setContentText(text); startForeground(4, builder.build()); }
    猜你喜欢
    • 2018-12-20
    • 2018-11-15
    • 1970-01-01
    • 2017-09-01
    • 2011-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    相关资源
    最近更新 更多