android - 通知中的操作不执行任何操作

标签 android service notifications android-pendingintent remote-input

这是我的代码:

  Intent notificationIntent = new Intent(this, PSTimelineFragmentsActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(PSLocationService.this);
    mBuilder.setPriority(Notification.PRIORITY_MAX);
    mBuilder.setContentTitle(getText(R.string.passenger_name_long));
    mBuilder.setContentText(getText(R.string.service_subtitle));
    mBuilder.setSmallIcon(R.drawable.notification_icon);
    mBuilder.setContentIntent(pendingIntent);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
        Intent action1Intent = new Intent(PSLocationService.this, NotificationActionService.class)
                .setAction("autopilot").putExtra("autopilot", "autopilot");
        PendingIntent action1PendingIntent = PendingIntent.getService(PSLocationService.this, 0,
                action1Intent, PendingIntent.FLAG_ONE_SHOT);

        Intent action2Intent = new Intent(PSLocationService.this, NotificationActionService.class)
                .setAction("settings").putExtra("settings", "settings");
        PendingIntent action2PendingIntent = PendingIntent.getService(PSLocationService.this, 1,
                action2Intent, PendingIntent.FLAG_ONE_SHOT);

        RemoteInput remoteInput = new RemoteInput.Builder("autopilot")
                .setLabel("Stop")
                .build();
        NotificationCompat.Action action1 =
                new NotificationCompat.Action.Builder(R.drawable.icn_terms_settings,
                        "Stop", action1PendingIntent)
                        .addRemoteInput(remoteInput)
                        .build();

        RemoteInput remoteInput2 = new RemoteInput.Builder("settings")
                .setLabel("Settings")
                .build();
        NotificationCompat.Action action2 =
                new NotificationCompat.Action.Builder(R.drawable.icn_terms_settings,
                        "Settings", action2PendingIntent)
                        .addRemoteInput(remoteInput2)
                        .build();
        mBuilder.addAction(action1);
        mBuilder.addAction(action2);
    }
    Notification notification = mBuilder.build();
    startForeground(1338, notification);

但每当我按下通知时,我都会收到此错误:

12-15 17:35:06.553: D/StatusBar(933): Clicked on button 0 for 0|nl.hgrams.passenger|1338|null|12567
12-15 17:35:06.555: E/RemoteViews(933): Cannot send pending intent: 
12-15 17:35:06.555: E/RemoteViews(933): android.content.IntentSender$SendIntentException
12-15 17:35:06.555: E/RemoteViews(933):     at android.app.ContextImpl.startIntentSender(ContextImpl.java:753)
12-15 17:35:06.555: E/RemoteViews(933):     at android.content.ContextWrapper.startIntentSender(ContextWrapper.java:390)
12-15 17:35:06.555: E/RemoteViews(933):     at android.content.ContextWrapper.startIntentSender(ContextWrapper.java:390)
12-15 17:35:06.555: E/RemoteViews(933):     at android.widget.RemoteViews$OnClickHandler.onClickHandler(RemoteViews.java:217)
12-15 17:35:06.555: E/RemoteViews(933):     at com.android.systemui.statusbar.BaseStatusBar$3.onClickHandler(BaseStatusBar.java:323)
12-15 17:35:06.555: E/RemoteViews(933):     at android.widget.RemoteViews$SetOnClickPendingIntent$1.onClick(RemoteViews.java:720)
12-15 17:35:06.555: E/RemoteViews(933):     at android.view.View.performClick(View.java:5204)
12-15 17:35:06.555: E/RemoteViews(933):     at android.view.View$PerformClick.run(View.java:21153)
12-15 17:35:06.555: E/RemoteViews(933):     at android.os.Handler.handleCallback(Handler.java:739)
12-15 17:35:06.555: E/RemoteViews(933):     at android.os.Handler.dispatchMessage(Handler.java:95)
12-15 17:35:06.555: E/RemoteViews(933):     at android.os.Looper.loop(Looper.java:148)
12-15 17:35:06.555: E/RemoteViews(933):     at android.app.ActivityThread.main(ActivityThread.java:5417)
12-15 17:35:06.555: E/RemoteViews(933):     at java.lang.reflect.Method.invoke(Native Method)
12-15 17:35:06.555: E/RemoteViews(933):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
12-15 17:35:06.555: E/RemoteViews(933):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

最佳答案

FLAG_ONE_SHOT 更改为 FLAG_UPDATE_CURRENT 使其正常工作

关于android - 通知中的操作不执行任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47836447/

相关文章:

xcode - 带有 XCode 6 Beta 7 的 Swift 在注册通知方面有问题

android - VS Code/Flutter/Android - keystore 文件未设置用于签署配置版本

ios - 在 iOS 上使用多个崩溃报告器

java - Android-如何在服务和命令行之间进行通信

c# - 安全使用 'HttpContext.Current.Cache'

android - 如何静默添加通知?

python - 你如何在 Mac 上收听来自 iTunes 的通知(使用 NSDistributedNotificationCenter)

java - 为什么 SimpleDateFormat 将 unix 日期显示为 1969 年 12 月 31 日?

android - 了解 invoke-kind/range 参数字数

java - 如何从这两个数组生成一组指定数量的数字?