我已经制作了custom notification并且有一个按钮,我想functionalities on notification and button click执行两个不同的functionalities on notification and button click 。 我看了很多链接但找不到添加按钮监听器的方法。

谁能帮忙。 这是我的代码。 非常感谢。private void startNotification() { Intent intent; PendingIntent pIntent; RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.mynotification); Context context = getApplicationContext(); NotificationCompat.Builder builder = new NotificationCompat.Builder( this).setSmallIcon(R.drawable.ic_launcher).setContent( remoteViews); if (hasFlash) { intent = new Intent(context, FlashLight.class); pIntent = PendingIntent.getActivity(context, 1, intent, 0); } else { intent = new Intent(context, BlankWhiteActivity.class); pIntent = PendingIntent.getActivity(context, 1, intent, 0); } builder.setContentIntent(pIntent); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notif = builder.setContentTitle("Flashlight") .setContentText("Lighten your world!!!").build(); mNotificationManager.notify(1, notif); remoteViews.setOnClickPendingIntent(R.id.closeOnFlash, pIntent); }

我已经通过closeOnFlash的按钮id( closeOnFlash )不知道为什么它不起作用。

这是我的xml :

开始通知为:

private void startNotification(){ String ns = Context.NOTIFICATION_SERVICE; NotificationManager notificationManager = (NotificationManager) getSystemService(ns); Notification notification = new Notification(R.drawable.ic_launcher, null, System.currentTimeMillis()); RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.mynotification); //the intent that is started when the notification is clicked (works) Intent notificationIntent = new Intent(this, FlashLight.class); PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.contentView = notificationView; notification.contentIntent = pendingNotificationIntent; notification.flags |= Notification.FLAG_NO_CLEAR; //this is the intent that is supposed to be called when the //button is clicked Intent switchIntent = new Intent(this, switchButtonListener.class); PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0, switchIntent, 0); notificationView.setOnClickPendingIntent(R.id.closeOnFlash, pendingSwitchIntent); notificationManager.notify(1, notification); } public static class switchButtonListener extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.d("Here", "I am here"); FlashOnOff flashLight; flashLight = new FlashOnOff(); flashLight.flashLightOff(); flashLight.releaseCamera(); } }

使用的xml:

在Application标签下的清单中:

android 通知 按钮,在自定义通知中添加按钮操作相关推荐

  1. java swt 画按钮_向表中添加按钮(java swt)

    我正在尝试复制类似于此的UI: 我一直在关注如何创建表格每列中的按钮的作者说明(没有成功).我的项目与他的区别在于我正在尝试使用Tree而不是Table,而我正在使用eclipse TreeViewe ...

  2. 在GridView中添加按钮后,如何触发按钮的各种事件?

    //在GridView中添加按钮后,如何触发按钮的各种事件? 1.在GridView的"编辑列"属性窗口中,增加新的Botton列. 2.然后再该窗口右边属性中,将外观项下的But ...

  3. java表格怎么添加按钮_1.6.2 在表格中添加按钮

    1.6.2  在表格中添加按钮 表格用于显示复合数据,其中可以指定表格的表头和表文.默认的表格控件完全是  以文本方式显示目标数据,要实现在表格中添加按钮或其他组件就要通过设置自定义的  渲染器来实现 ...

  4. 如何在CSDN博客自定义栏目中添加“给我写信”

    在"自定义栏目"中添加"连接"(将自己的微博,QQ空间和CSDN博客关联起来)很多人都做过.但是添加"给我写信"这个功能,用的好像不太多.此 ...

  5. OAF在打开的新页面中添加按钮,功能是关闭当前页面

    OAF在打开的新页面中添加按钮,功能是关闭当前页面 javascript:close()

  6. java按钮添加事件_java中添加按钮并添加响应事件的方法(推荐)

    关于java容器,面板等自行百度学一下吧 private button loginbtn = new button("登陆"); final static jframe buyer ...

  7. java jtable 按钮_java web 怎么在jtable中添加按钮?

    展开全部 java web在jtable中添加按钮的示例如下:import java.awt.BorderLayout; import java.awt.Color; import java.awt. ...

  8. C#中的DataGridView中添加按钮并操作数据

    背景:最近在项目中有需求需要在DataGridView中添加"删除"."修改"按钮,用来对数据的操作以及显示. 在DataGridView中显示需要的按钮 首先 ...

  9. android 创建3个按钮,【记录】继续尝试给Android程序的右上角的ActionBar中添加三个点的选项按钮...

    [背景] 想要给Android中的ActionBar中添加那种三个点的选项菜单,之前已经折腾过了,但是失败了,以为新版Android中没法添加呢: 但是后来在别的4.1.2的Android手机中,也还 ...

最新文章

  1. 使用 xcworkspace 管理 iOS 工程
  2. Codeforces 757C - Felicity is Coming!
  3. 软件学院学习体验之三
  4. 【Python基础】关于Python的前后、单双下划线作用
  5. 斗地主案例的需求分析
  6. 【LeetCode笔记】剑指 Offer 57- II. 和为 s 的连续正数序列(Java、滑动窗口、二刷)
  7. 数据结构之链表(Linked list)
  8. vue3 Vite 2.0 Vue-Router 4.0 TypeScript elementPlus admin 须弥戒后台案例小结
  9. 【手把手带你Godot游戏开发】Godot界面介绍
  10. 【高德地图API】申请key全流程
  11. 计算学习理论PAC模型
  12. 专注要事、把手弄脏、高效优雅是对抗规模化焦虑的好办法--读Getting Real(达成现实)和 Rework(重塑工作)
  13. Linux:Linux文件属性
  14. 什么是数据库中的一对一关系?
  15. 用python编写一个猜年龄的小程序-知道了这个,你也能写出 Python 趣味小程序
  16. 计算机网络第八章ppt,计算机网络-第八章.ppt
  17. 环境python3+selenium3+火狐52版本
  18. 爱因斯坦唯一翻车的地方,量子纠缠太魔幻了,他始终不愿相信
  19. 深入理解Socket的读写
  20. android 关闭关机动画,Android 开关机动画 BootAnimation/ShutdownAnimation 解析

热门文章

  1. 聊一聊大火的对比学习
  2. oracle ora-01476: 除数为 0,Oracle常见错误:ORA-01403 的根本原因和解决方案
  3. 吴恩达《机器学习》第二章:线性回归
  4. 吴恩达机器学习 7.神经网络参数的反向传播算法
  5. 吴恩达深度学习神经网络基础编程作业Logistic Regression with a Neural Network mindset
  6. 求解斐波那契第n项的几种解法(含矩阵乘法+快速幂) Python实现
  7. Python爬虫基本原理
  8. mysql sysdate 格式化_MySQL函数汇总
  9. 进出仓原理_通达信浪口主图+窥窃天机副图+进出仓副图强强组合
  10. python面试自我介绍_如何拿到半数面试公司Offer——我的Python求职之路