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

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

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

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
mitkmikd

mitkmikd1#

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

相关问题