【问题标题】:Notification PendingIntent from BroadcastReceiver来自 BroadcastReceiver 的通知 PendingIntent
【发布时间】:2015-11-06 14:33:36
【问题描述】:

伙计们。

我已经调试了好几天了,但我找不到答案。

正如标题所说,我正在使用BroadcastReceiver 发出通知。

在我添加 .addAction 之前一切都很好。

默认需要PendingIntent,我创建了PendingIntent

但问题是这样的;当我添加.addAction 时,它会这样说

Incompatiable types.

Required : android.app.Notification

Found : android.app.Notification.Builder

代码在没有.addActionPendingIntent 的情况下工作得很好。

可能是什么问题?

这是我的代码,

  • BroadcastReceiver, WifiService.java

    Intent intent = new Intent(mcontext, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pIntent = PendingIntent.getActivity(mcontext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
    Notification noti = new Notification.Builder(mcontext)
            .setContentTitle("Welcome home!")
            .setContentText("You have a to-do; " + hometodo)
            .setSmallIcon(R.drawable.home_light)
            .setLargeIcon(icon)
            .setLights(0xFFFFD800, 5000, 0)
            .addAction(R.drawable.archive_notification, "Mark as done", pIntent);
            //.addAction gets the error.
            .build();
    
    NotificationManager notificationManager = (NotificationManager) mcontext.getSystemService(mcontext.NOTIFICATION_SERVICE);
    
    notificationManager.notify(0, noti);
    

【问题讨论】:

  • .addAction(R.drawable.archive_notification, "Mark as done", pIntent);删除;
  • 我觉得自己很笨;(谢谢..
  • 没有问题,欢迎您

标签: android android-intent android-notifications android-pendingintent


【解决方案1】:

使用NotificationCompat 设置.addAction(...)

查看official docs

操作按钮不会出现在 Android 4.1 之前的平台上。行动 按钮取决于扩展通知,这些通知仅在 Android 4.1 及更高版本。

【讨论】:

  • @BedrockDev 只是删除多余的;
  • @pskink 你真的有一个Eagle Eye。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多