java应用程序由于startforeground的错误通知而崩溃

n3schb8v  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(456)

我一直在夜间过滤器的应用程序,但我坚持与sdk版本。我的应用程序在sdk版本低于23但高于23的设备上运行得很好,就像我在小米上测试我的应用程序时,android 9应用程序崩溃了,当我跟踪logcat错误时,会通过声明startforeground的错误通知。看到这个logcat消息了吗

2021-01-05 13:24:04.295 17312-17312/delhisehai.eyecare E/AndroidRuntime: FATAL EXCEPTION: main
    Process: delhisehai.eyecare, PID: 17312
    android.app.RemoteServiceException: Bad notification for startForeground
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1760)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:201)
        at android.app.ActivityThread.main(ActivityThread.java:6823)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

这是我的服务活动

private void updateNotification() {
        Intent intent = new Intent((Context)this, MyBroadcastReceiver.class);
        intent.setAction("ACTION_SWTICH_FILTER");
        PendingIntent pendingIntent = PendingIntent.getBroadcast((Context)this, (int)0, (Intent)intent, (int)0);
        Intent intent2 = new Intent((Context)this, MyBroadcastReceiver.class);
        intent2.setAction("ADJUST_INTENSITY");
        intent2.putExtra("intensity", "add");
        PendingIntent pendingIntent2 = PendingIntent.getBroadcast((Context)this, (int)1, (Intent)intent2, PendingIntent.FLAG_UPDATE_CURRENT);
        Intent intent3 = new Intent((Context)this, MyBroadcastReceiver.class);
        intent3.setAction("ADJUST_INTENSITY");
        intent3.putExtra("intensity", "minus");
        PendingIntent pendingIntent3 = PendingIntent.getBroadcast((Context)this, (int)2, (Intent)intent3, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent pendingIntent4 = PendingIntent.getActivity((Context)this, (int)0, (Intent)new Intent((Context)this, MainActivity.class), (int)0);
        String string2 = getResources().getString(R.string.app_name);
        int n = MySharedPreferences.getAlpha((Context)this);
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout_notification);
        remoteViews.setTextViewText(R.id.notitext1, (CharSequence)string2);
        remoteViews.setTextViewText(R.id.notitext4, (CharSequence)((int)(100.0 * (double)n / 200.0) + "%"));
        remoteViews.setOnClickPendingIntent(R.id.btn1, pendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.btn2, pendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.notitext3, pendingIntent2);
        remoteViews.setOnClickPendingIntent(R.id.notitext45, pendingIntent3);
        if (filterIsOn) {
            remoteViews.setViewVisibility(R.id.notitext2, View.INVISIBLE);
            remoteViews.setViewVisibility(R.id.notilayout, View.VISIBLE);
            remoteViews.setViewVisibility(R.id.btn2, View.INVISIBLE);
            remoteViews.setViewVisibility(R.id.btn1, View.VISIBLE);
            remoteViews.setTextViewText(R.id.btn1, (CharSequence)this.getResources().getString(R.string.turn_on));
        } else {
            remoteViews.setViewVisibility(R.id.notitext2, View.VISIBLE);
            remoteViews.setViewVisibility(R.id.notilayout, View.INVISIBLE);
            remoteViews.setViewVisibility(R.id.btn2,  View.VISIBLE);
            remoteViews.setViewVisibility(R.id.btn1, View.INVISIBLE);
            remoteViews.setTextViewText(R.id.btn2, (CharSequence)this.getResources().getString(R.string.turn_off));
        }
        Notification notification = new NotificationCompat.Builder(getApplicationContext()).setOngoing(false).setSmallIcon(R.drawable.red1).setContentIntent(pendingIntent4).setPriority(2).setWhen(0L).setContent(remoteViews).build();
        if (!MySharedPreferences.getShowIcon((Context)this)) {
            notification = new NotificationCompat.Builder(getApplicationContext()).setOngoing(false).setSmallIcon(R.drawable.red1).setContentIntent(pendingIntent4).setPriority(-2).setWhen(0L).setContent(remoteViews).build();
        }
        startForeground(9216, notification);
    }

我已经使用远程视图显示通知和操纵那里只有像音乐播放器,但问题是如何解决这个错误。我也在清单中使用startforeground权限,但同样的问题是应用程序崩溃。我想我设置为9216的通知id有问题。所以有人能给我同样的解决方案吗。
提前谢谢!

jckbn6z7

jckbn6z71#

我理解这个问题。并非所有的源代码都与所有android设备兼容。就像你的源代码一样。你写了很多代码。所以我不能重新编辑你的源代码。我只是演示一下。与android设备进行兼容的通知。
你也可以在我的github上获得回购。

public void showNotification()
{
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,"channelID")
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContentTitle("Notification")
            .setContentText("Hello! This is a notification.")
            .setAutoCancel(true);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    int notificationId = 1;
    createChannel(notificationManager);
    notificationManager.notify(notificationId, notificationBuilder.build());
}

public void createChannel(NotificationManager notificationManager){
    if (Build.VERSION.SDK_INT < 26) {
        return;
    }
    NotificationChannel channel = new NotificationChannel("channelID","name", NotificationManager.IMPORTANCE_DEFAULT);
    channel.setDescription("Hello! This is a notification.");
    notificationManager.createNotificationChannel(channel);
}

相关问题