gpt4 book ai didi

java - 通知功能在某些设备上崩溃

转载 作者:行者123 更新时间:2023-12-01 13:17:49 24 4
gpt4 key购买 nike

我正在为我的应用程序制作通知栏,尽管它在 htc感觉上运行良好,但在 htcdesire 上不起作用任何人都可以告诉我为什么,这是我的代码:

    private void startNotification() {

remoteViews = new RemoteViews(getPackageName(),
R.layout.simple_notification);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher).setContent(
remoteViews);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = builder.setContentTitle("Flashlight")
.setContentText("Lighten your world!!!").build();
notificationManager.notify(1, notification);

// the intent that is started when the notification is clicked (works)
Intent notificationIntent = new Intent(this, FlashLight.class);
PendingIntent pendingMainNotificationIntent = PendingIntent
.getActivity(this, 0, notificationIntent, 0);

notification.contentIntent = pendingMainNotificationIntent;
notification.flags |= Notification.FLAG_NO_CLEAR;

// this is the intent that is supposed to be called when
// the ON/OFF buttons are clicked
Intent switchIntent = new Intent(this, switchOffButtonListener.class);
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0,
switchIntent, 0);

remoteViews.setOnClickPendingIntent(R.id.closeOnFlash,
pendingSwitchIntent);

Intent switchOnIntent = new Intent(this, switchOnButtonListener.class);
PendingIntent pendingSwitchOnIntent = PendingIntent.getBroadcast(this,
0, switchOnIntent, 0);

remoteViews.setOnClickPendingIntent(R.id.onCloseFlash,
pendingSwitchOnIntent);

notificationManager.notify(1, notification);
}

日志猫:

03-11 15:54:48.247: E/AndroidRuntime(5201): FATAL EXCEPTION: main
03-11 15:54:48.247: E/AndroidRuntime(5201): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.flashlight/com.example.flashlight.FlashLight}: java.lang.IllegalArgumentException: contentIntent required: pkg=com.example.flashlight id=1 notification=Notification(vibrate=null,sound=null,button=null,defaults=0x0,flags=0x0)
03-11 15:54:48.247: E/AndroidRuntime(5201): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872)
03-11 15:54:48.247: E/AndroidRuntime(5201): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
03-11 15:54:48.247: E/AndroidRuntime(5201): at android.app.ActivityThread.access$1500(ActivityThread.java:135)
03-11 15:54:48.247: E/AndroidRuntime(5201): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
03-11 15:54:48.247: E/AndroidRuntime(5201): at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 15:54:48.247: E/AndroidRuntime(5201): at android.os.Looper.loop(Looper.java:150)
03-11 15:54:48.247: E/AndroidRuntime(5201): at android.app.ActivityThread.main(ActivityThread.java:4389)
03-11 15:54:48.247: E/AndroidRuntime(5201): at java.lang.reflect.Method.invokeNative(Native Method)
03-11 15:54:48.247: E/AndroidRuntime(5201): at java.lang.reflect.Method.invoke(Method.java:507)
03-11 15:54:48.247: E/AndroidRuntime(5201): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
03-11 15:54:48.247: E/AndroidRuntime(5201): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
03-11 15:54:48.247: E/AndroidRuntime(5201): at dalvik.system.NativeStart.main(Native Method)
03-11 15:54:48.247: E/AndroidRuntime(5201): Caused by: java.lang.IllegalArgumentException: contentIntent required: pkg=com.example.flashlight id=1 notification=Notification(vibrate=null,sound=null,button=null,defaults=0x0,flags=0x0)
03-11 15:54:48.247: E/AndroidRuntime(5201): at android.os.Parcel.readException(Parcel.java:1326)
03-11 15:54:48.247: E/AndroidRuntime(5201): at android.os.Parcel.readException(Parcel.java:1276)
03-11 15:54:48.247: E/AndroidRuntime(5201): at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:394)
03-11 15:54:48.247: E/AndroidRuntime(5201): at android.app.NotificationManager.notify(NotificationManager.java:111)
03-11 15:54:48.247: E/AndroidRuntime(5201): at android.app.NotificationManager.notify(NotificationManager.java:91)
03-11 15:54:48.247: E/AndroidRuntime(5201): at com.example.flashlight.FlashLight.startNotification(FlashLight.java:363)
03-11 15:54:48.247: E/AndroidRuntime(5201): at com.example.flashlight.FlashLight.onCreate(FlashLight.java:87)
03-11 15:54:48.247: E/AndroidRuntime(5201): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
03-11 15:54:48.247: E/AndroidRuntime(5201): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
03-11 15:54:48.247: E/AndroidRuntime(5201): ... 11 more

最佳答案

我自己弄清楚,实际上在旧的android版本中,您的通知必须有一个内容 Intent ,这样当用户点击您的通知时,就会发生一些事情。

这是运行代码,这里必须添加 setContentIntent();

  private void startNotification() {

// In older android versions, your Notification has to have a content
// Intent, so that when the user clicks your Notification, something
// happens default functionality
Intent intent = new Intent(this, FlashLight.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pend = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);

// checking the device has flash or not

RemoteViews remoteViews;
if (!hasFlash) {
remoteViews = new RemoteViews(getPackageName(),
R.layout.simple_notification);
} else {
remoteViews = new RemoteViews(getPackageName(),
R.layout.mynotification);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContent(remoteViews).setContentIntent(pend);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = builder.setContentTitle("Flashlight")
.setContentText("Lighten your world!!!").build();
notificationManager.notify(1, notification);

// the intent that is started when the notification is clicked (works)
Intent notificationIntent = new Intent(this, FlashLight.class);
PendingIntent pendingMainNotificationIntent = PendingIntent
.getActivity(this, 0, notificationIntent, 0);

notification.contentIntent = pendingMainNotificationIntent;
notification.flags |= Notification.FLAG_NO_CLEAR;

// this is the intent that is supposed to be called when
// the ON/OFF buttons are clicked
Intent switchIntent = new Intent(this, switchOffButtonListener.class);
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0,
switchIntent, 0);

remoteViews.setOnClickPendingIntent(R.id.closeOnFlash,
pendingSwitchIntent);

Intent switchOnIntent = new Intent(this, switchOnButtonListener.class);
PendingIntent pendingSwitchOnIntent = PendingIntent.getBroadcast(this,
0, switchOnIntent, 0);

remoteViews.setOnClickPendingIntent(R.id.onCloseFlash,
pendingSwitchOnIntent);

notificationManager.notify(1, notification);
}

关于java - 通知功能在某些设备上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22323752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com