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

sc4hvdpw  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(593)

我正在为我的应用程序制作通知栏,虽然它在htc sensation上运行良好,但在htc desire上不起作用。有人能告诉我为什么吗,这是我的代码:

  1. private void startNotification() {
  2. remoteViews = new RemoteViews(getPackageName(),
  3. R.layout.simple_notification);
  4. NotificationCompat.Builder builder = new NotificationCompat.Builder(
  5. this).setSmallIcon(R.drawable.ic_launcher).setContent(
  6. remoteViews);
  7. NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  8. Notification notification = builder.setContentTitle("Flashlight")
  9. .setContentText("Lighten your world!!!").build();
  10. notificationManager.notify(1, notification);
  11. // the intent that is started when the notification is clicked (works)
  12. Intent notificationIntent = new Intent(this, FlashLight.class);
  13. PendingIntent pendingMainNotificationIntent = PendingIntent
  14. .getActivity(this, 0, notificationIntent, 0);
  15. notification.contentIntent = pendingMainNotificationIntent;
  16. notification.flags |= Notification.FLAG_NO_CLEAR;
  17. // this is the intent that is supposed to be called when
  18. // the ON/OFF buttons are clicked
  19. Intent switchIntent = new Intent(this, switchOffButtonListener.class);
  20. PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0,
  21. switchIntent, 0);
  22. remoteViews.setOnClickPendingIntent(R.id.closeOnFlash,
  23. pendingSwitchIntent);
  24. Intent switchOnIntent = new Intent(this, switchOnButtonListener.class);
  25. PendingIntent pendingSwitchOnIntent = PendingIntent.getBroadcast(this,
  26. 0, switchOnIntent, 0);
  27. remoteViews.setOnClickPendingIntent(R.id.onCloseFlash,
  28. pendingSwitchOnIntent);
  29. notificationManager.notify(1, notification);
  30. }

日志:

  1. 03-11 15:54:48.247: E/AndroidRuntime(5201): FATAL EXCEPTION: main
  2. 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)
  3. 03-11 15:54:48.247: E/AndroidRuntime(5201): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872)
  4. 03-11 15:54:48.247: E/AndroidRuntime(5201): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
  5. 03-11 15:54:48.247: E/AndroidRuntime(5201): at android.app.ActivityThread.access$1500(ActivityThread.java:135)
  6. 03-11 15:54:48.247: E/AndroidRuntime(5201): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
  7. 03-11 15:54:48.247: E/AndroidRuntime(5201): at android.os.Handler.dispatchMessage(Handler.java:99)
  8. 03-11 15:54:48.247: E/AndroidRuntime(5201): at android.os.Looper.loop(Looper.java:150)
  9. 03-11 15:54:48.247: E/AndroidRuntime(5201): at android.app.ActivityThread.main(ActivityThread.java:4389)
  10. 03-11 15:54:48.247: E/AndroidRuntime(5201): at java.lang.reflect.Method.invokeNative(Native Method)
  11. 03-11 15:54:48.247: E/AndroidRuntime(5201): at java.lang.reflect.Method.invoke(Method.java:507)
  12. 03-11 15:54:48.247: E/AndroidRuntime(5201): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
  13. 03-11 15:54:48.247: E/AndroidRuntime(5201): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
  14. 03-11 15:54:48.247: E/AndroidRuntime(5201): at dalvik.system.NativeStart.main(Native Method)
  15. 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)
  16. 03-11 15:54:48.247: E/AndroidRuntime(5201): at android.os.Parcel.readException(Parcel.java:1326)
  17. 03-11 15:54:48.247: E/AndroidRuntime(5201): at android.os.Parcel.readException(Parcel.java:1276)
  18. 03-11 15:54:48.247: E/AndroidRuntime(5201): at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:394)
  19. 03-11 15:54:48.247: E/AndroidRuntime(5201): at android.app.NotificationManager.notify(NotificationManager.java:111)
  20. 03-11 15:54:48.247: E/AndroidRuntime(5201): at android.app.NotificationManager.notify(NotificationManager.java:91)
  21. 03-11 15:54:48.247: E/AndroidRuntime(5201): at com.example.flashlight.FlashLight.startNotification(FlashLight.java:363)
  22. 03-11 15:54:48.247: E/AndroidRuntime(5201): at com.example.flashlight.FlashLight.onCreate(FlashLight.java:87)
  23. 03-11 15:54:48.247: E/AndroidRuntime(5201): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
  24. 03-11 15:54:48.247: E/AndroidRuntime(5201): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
  25. 03-11 15:54:48.247: E/AndroidRuntime(5201): ... 11 more
mitkmikd

mitkmikd1#

我自己想清楚在旧的android版本中,你的通知必须有一个内容意图,这样当用户点击你的通知时,就会发生一些事情。
这是运行代码在这里必须添加setcontentintent();

  1. private void startNotification() {
  2. // In older android versions, your Notification has to have a content
  3. // Intent, so that when the user clicks your Notification, something
  4. // happens default functionality
  5. Intent intent = new Intent(this, FlashLight.class);
  6. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  7. PendingIntent pend = PendingIntent.getActivity(this, 0, intent,
  8. PendingIntent.FLAG_UPDATE_CURRENT);
  9. // checking the device has flash or not
  10. RemoteViews remoteViews;
  11. if (!hasFlash) {
  12. remoteViews = new RemoteViews(getPackageName(),
  13. R.layout.simple_notification);
  14. } else {
  15. remoteViews = new RemoteViews(getPackageName(),
  16. R.layout.mynotification);
  17. }
  18. NotificationCompat.Builder builder = new NotificationCompat.Builder(
  19. this).setSmallIcon(R.drawable.ic_launcher)
  20. .setContent(remoteViews).setContentIntent(pend);
  21. NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  22. Notification notification = builder.setContentTitle("Flashlight")
  23. .setContentText("Lighten your world!!!").build();
  24. notificationManager.notify(1, notification);
  25. // the intent that is started when the notification is clicked (works)
  26. Intent notificationIntent = new Intent(this, FlashLight.class);
  27. PendingIntent pendingMainNotificationIntent = PendingIntent
  28. .getActivity(this, 0, notificationIntent, 0);
  29. notification.contentIntent = pendingMainNotificationIntent;
  30. notification.flags |= Notification.FLAG_NO_CLEAR;
  31. // this is the intent that is supposed to be called when
  32. // the ON/OFF buttons are clicked
  33. Intent switchIntent = new Intent(this, switchOffButtonListener.class);
  34. PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0,
  35. switchIntent, 0);
  36. remoteViews.setOnClickPendingIntent(R.id.closeOnFlash,
  37. pendingSwitchIntent);
  38. Intent switchOnIntent = new Intent(this, switchOnButtonListener.class);
  39. PendingIntent pendingSwitchOnIntent = PendingIntent.getBroadcast(this,
  40. 0, switchOnIntent, 0);
  41. remoteViews.setOnClickPendingIntent(R.id.onCloseFlash,
  42. pendingSwitchOnIntent);
  43. notificationManager.notify(1, notification);
  44. }
展开查看全部

相关问题