自定义Notification添加点击事件
来源:互联网 发布:淘宝退货率在哪里看 编辑:程序博客网 时间:2024/05/20 03:48
前言
在上一篇文章中《Notification自定义界面》中我们实现了自定义的界面,那么我们该怎么为自定义的界面添加点击事件呢?像酷狗在通知栏 有“上一首”,“下一首”等控制按钮,我们需要对按钮的点击事件进行响应,不过方法和之前的点击设置不一样,需要另外处理,下面我将进行简单的说明。
实现
同样,我们需要一个Service的子类MyService,然后在MyService的onCreate中设置,如下代码:
public class MyService extends Service { public static final String ONCLICK = "com.app.onclick"; private BroadcastReceiver receiver_onclick = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(ONCLICK)) { Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(1000); } } }; @Override public void onCreate() { super.onCreate(); Notification notification = new Notification(R.drawable.ic_launcher, "JcMan", System.currentTimeMillis()); RemoteViews view = new RemoteViews(getPackageName(),R.layout.notification); notification.contentView = view; IntentFilter filter_click = new IntentFilter(); filter_click.addAction(ONCLICK); //注册广播 registerReceiver(receiver_onclick, filter_click); Intent Intent_pre = new Intent(ONCLICK); //得到PendingIntent PendingIntent pendIntent_click = PendingIntent.getBroadcast(this, 0, Intent_pre, 0); //设置监听 notification.contentView.setOnClickPendingIntent(R.id.btn,pendIntent_click); //前台运行 startForeground(1, notification); } @Override public IBinder onBind(Intent intent) { return null; }}
可以看到,我们先得到BroadcastReceiver的一个对象,然后在onReceiver里面实现我们的操作,我设置成点击时候手机震动一秒钟,当然不要忘记在配置文件添加震动的权限,不然到时候就会出错了。如果对广播没有了解的,那么可以先去了解一下广播的机制,这里我使用的是动态注册广播的方法,还有另外一种方法来注册,不过我更喜欢动态注册的罢了。
小结
看到在Notification添加一个ProgressBar来实现下载的进度提示,这里需要用到更新Notification界面的知识,虽然和在Activity中更新界面不太一样,但是也不是在复杂,因为我并没有用到这方面的知识,所以这里就不给大家介绍了,有兴趣的可以搜相关的内容。
0 0
- 自定义Notification添加点击事件
- 自定义Notification实现按钮点击事件
- 自定义View添加点击事件
- Android自定义View与添加点击事件
- Android 点击Notification事件处理
- notification几种点击事件
- 自定义view系列(3)--给自定义View添加点击事件
- Notification点击事件和点击消失
- 百度地图API自定义覆盖物与添加点击事件
- easyUI datagrid自定义添加列 (点击事件)
- android actionbar自定义View并添加点击事件
- UIView 添加点击事件
- xcode添加点击事件
- Flash添加点击事件
- UILabel添加点击事件
- UIimageView添加点击事件
- echarts添加点击事件
- UILabel添加点击事件
- Pinta--一个画图软件
- JAVA学习第十天
- linux中的nm命令简介
- HDU-1022-火车进出站问题 .
- 【Matlab学习笔记】【函数学习】max()和max(max())的区别
- 自定义Notification添加点击事件
- Gym 100703K Word order 简单贪心
- spring利用ApplicationListener自启动
- CHttpFile POST 时 Cookie 疑问
- [LeetCode] Unique Paths
- Axure 倒计时使用变量
- Android性能优化技巧
- iOS tableView的刷新 笔记
- Android 性能优化之使用MAT分析内存泄露问题