android发通知NotificationManager和Notification的详解
2015-09-10 14:33
417 查看
下面对Notification类中的一些常量,字段,方法简单介绍一下:
常量:
DEFAULT_ALL 使用所有默认值,比如声音,震动,闪屏等等
DEFAULT_LIGHTS 使用默认闪光提示
DEFAULT_SOUNDS 使用默认提示声音
DEFAULT_VIBRATE 使用默认手机震动
【说明】:加入手机震动,一定要在manifest.xml中加入权限:
<uses-permission android:name="android.permission.VIBRATE" />
以上的效果常量可以叠加,即通过
notification.defaults =DEFAULT_SOUND|DEFAULT_VIBRATE;
notification.defaults |= DEFAULT_SOUND (最好在真机上测试,震动效果模拟器上没有)
//设置flag位
FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉
FLAG_NO_CLEAR 该通知能被状态栏的清除按钮给清除掉
FLAG_ONGOING_EVENT 通知放置在正在运行
FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应
常用字段:
contentIntent 设置PendingIntent对象,点击时发送该Intent
defaults 添加默认效果
flags 设置flag位,例如FLAG_NO_CLEAR等
icon 设置图标
sound 设置声音
tickerText 显示在状态栏中的文字
when 发送此通知的时间戳
String appName = context.getResources().getString(R.string.app_name); Intent intent = new Intent(context, HomeActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); //这里需要设置Intent.FLAG_ACTIVITY_NEW_TASK属性
PendingIntent pend = PendingIntent.getActivity(context, R.string.app_name, intent, PendingIntent.FLAG_UPDATE_CURRENT);
常量:
DEFAULT_ALL 使用所有默认值,比如声音,震动,闪屏等等
DEFAULT_LIGHTS 使用默认闪光提示
DEFAULT_SOUNDS 使用默认提示声音
DEFAULT_VIBRATE 使用默认手机震动
【说明】:加入手机震动,一定要在manifest.xml中加入权限:
<uses-permission android:name="android.permission.VIBRATE" />
以上的效果常量可以叠加,即通过
notification.defaults =DEFAULT_SOUND|DEFAULT_VIBRATE;
notification.defaults |= DEFAULT_SOUND (最好在真机上测试,震动效果模拟器上没有)
//设置flag位
FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉
FLAG_NO_CLEAR 该通知能被状态栏的清除按钮给清除掉
FLAG_ONGOING_EVENT 通知放置在正在运行
FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应
常用字段:
contentIntent 设置PendingIntent对象,点击时发送该Intent
defaults 添加默认效果
flags 设置flag位,例如FLAG_NO_CLEAR等
icon 设置图标
sound 设置声音
tickerText 显示在状态栏中的文字
when 发送此通知的时间戳
public static void notice(String text) { if (!TextUtils.isEmpty(text)) { Context context = APPContext.getInstance(); NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(); notification.icon = R.mipmap.ic_launcher;// 设置通知的图标 notification.tickerText = text; // 显示在状态栏中的文字 notification.when = System.currentTimeMillis(); // 设置来通知时的时间 notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.defaults = Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE;//将使用默认的声音和振动来提醒用户
String appName = context.getResources().getString(R.string.app_name); Intent intent = new Intent(context, HomeActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); //这里需要设置Intent.FLAG_ACTIVITY_NEW_TASK属性
PendingIntent pend = PendingIntent.getActivity(context, R.string.app_name, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//这里必需要用setLatestEventInfo(上下文,标题,内容,PendingIntent)不然会报错.notification.setLatestEventInfo(context, appName, text, pend); manager.notify(getInstance().getIndex(), notification);
//这里发送通知(消息ID,通知对象)}}
相关文章推荐
- Android开发之旅
- Android studio问题
- android EditText inputType 中文解说
- Android AlertDialog setContentView输入法不显示
- android Gradle多渠道打包
- android 签名处理(已安装了存在签名冲突的同名数据包)
- Android studio 中使用GeenDao
- Android Studio加载jar不完整的解决办法
- FFmpeg for Android compiled with x264, libass, fontconfig, freetype and fribidi
- Icon Font --Android图标也能轻松换肤
- 允许Android随着屏幕转动的控制自由转移到任何地方(附demo)
- Android基础入门教程——7.4 Android调用WebService
- Android 笔试-概率题
- Android Layout布局文件里的android:layout_height等属性为什么会不起作用?
- Android动态换肤开源库Colorful发布
- [Android] Android开发优化之——使用软引用和弱引用
- Android style
- Android drawable selector foucus,pressed,selected的区别
- android View 绘制机制
- [Android] Android开发优化之——使用软引用和弱引用