首页 > 解决方案 > 后台服务在 API 28 上使应用程序崩溃,但在 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)

标签: javaandroidandroid-9.0-pie

解决方案


从 API 28 开始,如果你想设置前台服务,你必须添加FOREGROUND_SERVICE权限。

这是官方文档

面向 Android 9 或更高版本并使用前台服务的应用必须请求 FOREGROUND_SERVICE 权限。这是一个正常的权限,因此系统会自动将其授予请求的应用程序。如果针对 Android 9 或更高版本的应用尝试创建前台服务而不请求 FOREGROUND_SERVICE,则系统会引发 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);
    }


推荐阅读