notification几种点击事件
来源:互联网 发布:java开源加密工具 编辑:程序博客网 时间:2024/05/20 04:46
notification :以另一种UI界面形式展示出来,点击时可以对其应用做处理。
其常用的一些基本属性:setSmallIcon()设置图标 、setContentTitle()设置题目、setContentText()设置内容、setAutoCancel()设置点击后是否自动销毁、setTicker()设置通知时的滚动文本
1、构造通知、跳转界面
// 构造通知界面NotificationCompat.Builder builder = new NotificationCompat.Builder(NotifyActivity.this);builder.setSmallIcon(R.drawable.ic_launcher).setContentTitle(getString(R.string.hello_world)).setContentText(getString(R.string.hint_text)).setAutoCancel(true) // 点击跳转后自动销毁.setTicker(getString(R.string.hello_world)); // 设置滚动文本// 通知行为(点击后能进入应用界面)Intent intent = new Intent(NotifyActivity.this,Marquee_Test.class);PendingIntent pendingIntent = PendingIntent.getActivity(NotifyActivity.this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);builder.setContentIntent(pendingIntent);// 发送通知NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);manager.notify(1, builder.build()); // id不变则只显示一个,若id变化则可以重复显示多个
2、导航功能,返回应用首页
此时若点击返回时则直接跳出应用,若想其不退出应用还应在manifest中注册其父类路径(尽量使用4.1以上版本的模拟器,以下容易出错。)<activity android:name="com.example.ui.widget.Marquee_Test" <!-- 记住应在跳转界面下注册其父类 -->
android:parentActivityName="com.example.layout.LayoutAll"> <!-- 1、注册方式一支持4.1以上版本 -->
<meta-data
android:name=
"android.support.PARENT_ACTIVITY"<!-- 2、注册方式二 -->
android:value=
"com.example.layout.LayoutAll" />
此时只改变上方通知行为部分即可
<span style="white-space:pre"></span>Intent intent = new Intent(NotifyActivity.this,Marquee_Test.class); //要跳转的界面TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(NotifyActivity.this);taskStackBuilder.addParentStack(Marquee_Test.class); //参数为跳转界面taskStackBuilder.addNextIntent(intent);PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);builder.setContentIntent(pendingIntent);3、BigView 系统按钮监听事件
此时也只用改变上方通知行为部分即可
<span style="white-space:pre"></span>//播放部分
<span style="white-space:pre"></span>Intent playIntent = new Intent(NotifyActivity.this,PlayMusicService.class);playIntent.setAction("1");playIntent.putExtra("PATH","file://"+ Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+ "/wy.mp3");playIntent.putExtra("FLAG", 1);PendingIntent playPending = PendingIntent.getService(NotifyActivity.this, 0, playIntent,PendingIntent.FLAG_UPDATE_CURRENT);<span style="white-space:pre"></span>//暂停部分Intent pauseIntent = new Intent(NotifyActivity.this,PlayMusicService.class);pauseIntent.putExtra("PATH","file://"+ Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+ "/wy.mp3"); //将播放文件传递到服务端pauseIntent.setAction("2"); //此时必须设置action,不然不能播放,单个控件时可以不用设置,参数自定义pauseIntent.putExtra("FLAG", 2); //判断播放、暂停标记PendingIntent pausePending = PendingIntent.getService(NotifyActivity.this, 0, pauseIntent,PendingIntent.FLAG_UPDATE_CURRENT);builder.addAction(R.drawable.bg_actionbar, "播放", playPending); //系统添加按钮控件builder.addAction(R.drawable.bg_actionbar, "暂停", pausePending);4、自定义通知界面
<span style="white-space:pre"></span>NotificationCompat.Builder builder = new NotificationCompat.Builder(NotifyActivity.this);builder.setSmallIcon(R.drawable.ic_launcher); //此条件必须设置,不然不能显示builder.setTicker("开始播放音乐");RemoteViews view = new RemoteViews(getPackageName(), R.layout.notify_play_music_layout); //自定义布局builder.setContent(view);Intent playIntent = new Intent(NotifyActivity.this,PlayMusicService.class); //启动服务playIntent.setAction("1");playIntent.putExtra("PATH","file://"+ Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+ "/wy.mp3");playIntent.putExtra("FLAG", 1);PendingIntent playPending = PendingIntent.getService(NotifyActivity.this, 0, playIntent,PendingIntent.FLAG_UPDATE_CURRENT);view.setOnClickPendingIntent(R.id.music_play_img, playPending); //设置监听事件NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);manager.notify(4, builder.build());
0 0
- notification几种点击事件
- Android 点击Notification事件处理
- 自定义Notification添加点击事件
- Notification点击事件和点击消失
- android中的几种点击事件
- 点击事件的几种写法
- 点击事件的几种写法
- 【Android】点击事件的几种方法
- [按钮点击事件的几种方法]
- 几种触发点击事件的方法
- 自定义Notification实现按钮点击事件
- Android开发 按钮点击事件的几种写法
- 拨号器和几种点击事件的写法
- 安卓基础-点击事件的几种写法
- andorid基础之点击事件绑定的几种…
- Android中Button的几种点击事件写法
- Android Button响应点击事件的几种方法大合集
- <初级>安卓中事件点击的几种方式
- python 函数学习
- 普通树的存储与遍历(合集)
- hdu Flood-it!(IDA*算法)
- c++学习log1
- 深入理解计算机系统(2)
- notification几种点击事件
- 图片列表换行错位,图片无法左对齐怎么办
- 第3章 一切基于pom
- 推荐系统小结
- WEBSERVICE各家之说
- Unity3D开发技巧
- 基于opencv的相机之马赛克功能实现(九)
- 不要自称是程序员,我十多年的 IT 职场总结 http://blog.jobbole.com/94148/
- java基础内存分析