后台服务在 API 28 上使应用程序崩溃,但在 API 23 上运行

Background service crashes application on API 28 but works on API 23

我有这个应用程序在后台启动服务来监控接近传感器。这一切在 targetSdkVersion 23 上都很好用,但在 28 上就不行了。 服务:

public class ForegroundService (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {
            Log.i(LOG_TAG, "Received Start Foreground Intent ");
            Intent notificationIntent = new Intent(this, MainActivity.class);
            notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                    notificationIntent, 0);

            Intent previousIntent = new Intent(this, ForegroundService.class);
            previousIntent.setAction(Constants.ACTION.PREV_ACTION);
            PendingIntent ppreviousIntent = PendingIntent.getService(this, 0,
                    previousIntent, 0);

            Intent playIntent = new Intent(this, ForegroundService.class);
            playIntent.setAction(Constants.ACTION.PLAY_ACTION);
            PendingIntent pplayIntent = PendingIntent.getService(this, 0,
                    playIntent, 0);


            Intent 

当应用程序崩溃时,我也会收到 logcat:

020-01-03 03:24:00.466
26636-26636/com.personal.ramakrishnanak.autoscreenoff
E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.personal.ramakrishnanak.autoscreenoff, PID: 26636
        android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for
service notification: Notification(channel=null pri=0 contentView=null
vibrate=null sound=null tick defaults=0x0 flags=0x42 color=0x00000000
actions=1 vis=PRIVATE semFlags=0x0 semPriority=0 semMissedCount=0)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2067)
            at android.os.Handler.dispatchMessage(Handler.java:107)
            at android.os.Looper.loop(Looper.java:237)
            at android.app.ActivityThread.main(ActivityThread.java:7762)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)

来自API 28、如果要设置Foreground服务,必须添加FOREGROUND_SERVICE权限。

这是official document:

Apps that target Android 9 or higher and use foreground services must request the FOREGROUND_SERVICE permission. This is a normal permission, so the system automatically grants it to the requesting app. If an app that targets Android 9 or higher attempts to create a foreground service without requesting FOREGROUND_SERVICE, the system throws a SecurityException.

另一个问题可能是通知。来自API 26+,您必须添加通知渠道。

private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String CHANNEL_ID = "some_channel_id";
        CharSequence name = getString(R.string.channel_name);
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        // add the NotificationChannel
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }

您的通知似乎没有使用 NotificationChannel。这是 Android 8 岁以上的要求。

请参阅文档:https://developer.android.com/training/notify-user/channels

当您定位 Android 8.0(API 级别 26)时,您必须实施一个或多个通知渠道。

您必须有这样的代码才能在使用前创建频道

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    // Create the NotificationChannel
    String name = "channel_id";
    String descriptionText = "channel_description";
    int importance = NotificationManager.IMPORTANCE_DEFAULT;
    NotificationChannel channel = new NotificationChannel("channel_id", name, importance);
    channel.setDescription(descriptionText);
    NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    notificationManager.createNotificationChannel(channel);
}

通常您将代码放在自定义应用程序中 class。

那么你的通知生成器应该是这样的

Notification notification = new Notification.Builder(this,"channel_id")