android - 如何在自定义通知中的按钮上实现 onclick()

标签 android notifications

我正在尝试借助带有按钮的远程 View 来创建自定义通知。我想为此按钮编写 onclick 监听器,但问题是通知容器正在捕获触摸事件,并且按钮没有被单击。我的问题是有什么方法可以禁用此通知并让按钮被单击。我的代码如下所示:

Notification notification = new Notification(R.drawable.applogo,
        "Apps activated", System.currentTimeMillis());

Intent notificationIntent = new Intent(getBaseContext(), AppsActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(),
0, notificationIntent , 0);
notification.contentIntent = pendingIntent;

//Remoteview and intent for my button
RemoteViews remoteView = new RemoteViews(getBaseContext().getPackageName(),
R.layout.customnotification);

Intent mIntent = getPackageManager().getLaunchIntentForPackage(c.getString(1));//starting some application through package
PendingIntent pintent = PendingIntent.getActivity(this, 0,
                    mIntent, 0);

remoteView.setOnClickPendingIntent(R.id.button1,
pintent);

notification.contentView = remoteView;

notificationManager.notify(CUSTOM_NOTIFICATION_ID, notification);

我正在开发 API 7。 谢谢

最佳答案

不幸的是,通知布局中的可点击按钮仅适用于 Custom notifications对于 API >= 11。

此外,对于 API >= 16,您可以使用 Notification Actions在通知中提供按钮。

关于android - 如何在自定义通知中的按钮上实现 onclick(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12798185/

相关文章:

error-handling - 运行kaa通知演示

Android-未知的 webview 崩溃原因

android - Android应用错误:未找到Kotlin类,但仅在Release apk上

android - 在 Android 中使用 Alpha channel 解码 VP9,可能吗?

c# - Windows 10通用应用程序。如何触发后台任务?

ios - 使用 UIDatePicker 在特定时间停止通知

android - 如何让ListView的行满屏?

java - 使用 Android SIP 堆栈注册 SIP 配置文件

android - 必要时在默认通知声音上播放我的声音

ios - 如何在应用程序委托(delegate)之外注册本地通知?