由于 startforeground 的错误通知导致应用程序崩溃
App crashes due to bad notification for startforeground
我一直在开发夜间过滤器应用程序,但我坚持使用 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)
这是我的服务activity
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);
}
我已经使用 remoteviews 来显示通知并像音乐播放器一样在那里操作,但问题是如何解决这个错误。我还在清单中使用了 startforeground 权限,但同样的问题是应用程序崩溃。我认为我设置为 9216 的 Notification ID 有问题。所以任何人都可以给我解决方案。
提前致谢!
我明白这个问题。每个源代码并不与每个 android 设备兼容。就像您的源代码一样。你写了很多代码。这就是为什么我不能重新编辑你的整个源代码。我只是在做一个演示。与 android 台设备进行兼容通知。
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);
}
我一直在开发夜间过滤器应用程序,但我坚持使用 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)
这是我的服务activity
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);
}
我已经使用 remoteviews 来显示通知并像音乐播放器一样在那里操作,但问题是如何解决这个错误。我还在清单中使用了 startforeground 权限,但同样的问题是应用程序崩溃。我认为我设置为 9216 的 Notification ID 有问题。所以任何人都可以给我解决方案。
提前致谢!
我明白这个问题。每个源代码并不与每个 android 设备兼容。就像您的源代码一样。你写了很多代码。这就是为什么我不能重新编辑你的整个源代码。我只是在做一个演示。与 android 台设备进行兼容通知。
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);
}