android中的Notification(通知)
2015-08-30 17:34
525 查看
本篇介绍Notification的用法,它是一个通知栏(类似手机下拉界面的推送)
NotificationManager引用(OnCreate)
NotificationManager引用(OnCreate)
mNatifycatinManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
点击一个button添加通知,点击第二个button取消通知
用builder创建Notification
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)//build这个方法在16版本以前不能用,确定版本 @Override public void onClick(View v) { switch (v.getId()){ case R.id.button_notyfi: Intent intent=new Intent(getApplicationContext(),MainActivity.class); PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(), 1, intent, PendingIntent.FLAG_ONE_SHOT);//设置PendingIntent用法 Notification notification=new Notification.Builder(MainActivity.this).setAutoCancel(true).自动取消 setContentInfo("提示").setContentText("内容").setContentTitle("标题").setSmallIcon(R.mipmap.ic_launcher).图标 setContentIntent(pendingIntent).build(); mNatifycatinManager.notify(1,notification); break; case R.id.button_cancel://dier个按钮取消通知 mNatifycatinManager.cancel(1);//点一下取消,1是标志 break; } }
原来的版本(很麻烦,新版本优化了步骤)
@Override public void onClick(View v) { switch (v.getId()){ case R.id.button_notyfi: Notification notification=new Notification(); notification.icon=R.mipmap.ic_launcher;//放状态栏图片 notification.tickerText="我是一个消息";//放状态栏内容 notification.flags=Notification.FLAG_AUTO_CANCEL;//设置为可取消 Intent intent=new Intent(getApplicationContext(),MainActivity.class); PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(),1,intent, PendingIntent.FLAG_ONE_SHOT); //设置PendingIntent事件 notification.setLatestEventInfo(getApplicationContext(),"我是标题","我是内容",pendingIntent); notification.when= Calendar.getInstance().getTimeInMillis();//发送时的时间 //pending的显示内容 mNatifycatinManager.notify(1,notification);//添加通知 break; case R.id.button_cancel://dier个按钮取消通知 mNatifycatinManager.cancel(1);//点一下取消,1是标志 break; }
相关文章推荐
- Android可滑动的Tab页
- android Bitmap内存优化(二) 多线程下载图片
- 第一个android客户端笔记
- Android Drawable系列(1):自定义背景以及注意事项
- Android四大组件之Service
- Android 开源框架Universal-Image-Loader完全解析(一)--- 基本介绍及使用
- Android开发——Activity生命周期中的一些注意点
- android播放音频文件(MediaPlayer)和录音(MediaRecorder)--播放音频文件
- Android-Android studio 出现 Error: NDK integration is deprecated in the current plugin. 问题解决
- android开发环境搭建
- Android自学笔记之ToggleButton(开关按钮)的功能、特殊属性、用法
- android无法运行 adb.exe和模拟器不显示hello word
- Android数据缓存方案
- 我总结的Android编程规范
- Android-手势识别(自定义手势识别:四种自定义手势)
- Android常用加密算法比较
- Activity的启动模式
- 转发离线安装 Android Studio 更新
- Android(java)学习笔记203:网页源码查看器(Handler消息机制)
- DES的加密与解密(可逆)