android开发之Notification结合PendingIntent使用小结
2013-04-26 11:38
561 查看
一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个快讯,这时手从上方滑动状态栏就可以展开并处理这个快讯。发现这个功能特别好用。
使用小结:
1。获得系统服务NotificationMangaer:
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
2。创建Notification对象 【三个参数的意义分别为,通知的图片,状态栏中通知的滚动信息,什么时候展示该通知。】
Notification notification = new Notification(R.drawable.ic_launcher, "通知栏标题", System.currentTimeMillis());
3。设置flags参数:
notification.flags = Notification.FLAG_AUTO_CANCEL; // Notification.FLAG_NO_CLEAR【表明在点击了通知栏中的"清除通知"后,此通知不清除】
通知时发出声音
notification.defaults = Notification.DEFAULT_SOUND;
4。创建intent对象:
Intent intent = new Intent(MainActivity.this, Notifications.class);
如果要以该Intent启动一个Activity,一定要设置 Intent.FLAG_ACTIVITY_NEW_TASK 标记。
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NEW_TASK);
|————>Intent.FLAG_ACTIVITY_CLEAR_TOP :如果在当前Task中,有要启动的Activity,那么把该Acitivity之前的所有Activity都关掉,并把此Activity置前以避免创建Activity的实例。
|
|
|————>Intent.FLAG_ACTIVITY_NEW_TASK :系统会检查当前所有已创建的Task中是否有该要启动的Activity的Task,若有,则在该Task上创建Activity,若没有则新建具有该Activity属性的 | Task,并在该新建的Task上创建Activity。
5。 通过context对象和intent对象创建PendingIntent:
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0, intent, 0);
6.设置下拉框信息:【参数依次为:Context,标题,内容,PendingIntent。】
notification.setLatestEventInfo(MainActivity.this,"下拉框通知的标题","下拉框通知的内容", pendingIntent);
7.启动Notification:
manager.notify(0, notification);
参数依次为:在你的程序中标识Notification的id值(用来区分同一程序中的不同Notifycation,如果程序中只有一个 Notification那么这里随便你填什么都可以,不过类型必须要为int),要通知的Notification
使用小结:
1。获得系统服务NotificationMangaer:
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
2。创建Notification对象 【三个参数的意义分别为,通知的图片,状态栏中通知的滚动信息,什么时候展示该通知。】
Notification notification = new Notification(R.drawable.ic_launcher, "通知栏标题", System.currentTimeMillis());
3。设置flags参数:
notification.flags = Notification.FLAG_AUTO_CANCEL; // Notification.FLAG_NO_CLEAR【表明在点击了通知栏中的"清除通知"后,此通知不清除】
通知时发出声音
notification.defaults = Notification.DEFAULT_SOUND;
4。创建intent对象:
Intent intent = new Intent(MainActivity.this, Notifications.class);
如果要以该Intent启动一个Activity,一定要设置 Intent.FLAG_ACTIVITY_NEW_TASK 标记。
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NEW_TASK);
|————>Intent.FLAG_ACTIVITY_CLEAR_TOP :如果在当前Task中,有要启动的Activity,那么把该Acitivity之前的所有Activity都关掉,并把此Activity置前以避免创建Activity的实例。
|
|
|————>Intent.FLAG_ACTIVITY_NEW_TASK :系统会检查当前所有已创建的Task中是否有该要启动的Activity的Task,若有,则在该Task上创建Activity,若没有则新建具有该Activity属性的 | Task,并在该新建的Task上创建Activity。
5。 通过context对象和intent对象创建PendingIntent:
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0, intent, 0);
6.设置下拉框信息:【参数依次为:Context,标题,内容,PendingIntent。】
notification.setLatestEventInfo(MainActivity.this,"下拉框通知的标题","下拉框通知的内容", pendingIntent);
7.启动Notification:
manager.notify(0, notification);
参数依次为:在你的程序中标识Notification的id值(用来区分同一程序中的不同Notifycation,如果程序中只有一个 Notification那么这里随便你填什么都可以,不过类型必须要为int),要通知的Notification
相关文章推荐
- Android开发艺术探索<Notification使用>
- Android开发(18)--NotiFication详解与使用
- Android 开发笔记二 Notification 的使用
- Android开发教程:shape和selector的结合使用
- android-pulltorefesh快速开发框架的使用小结
- Android开发教程:shape和selector的结合使用
- android开发 友盟推送点击Notification使用WebView加载url同时会调用系统浏览器的问题
- Android开发教程:shape和selector的结合使用
- Android开发之使用Notification.Builder
- Android开发教程:shape和selector的结合使用
- Android开发教程:shape和selector的结合使用
- 【Android 进阶】原生Android结合H5混合开发小结
- 腾讯开发平台QQ登陆授权(Android_SDK_V3.1.0使用小结)
- Android状态栏通知Notification使用小结
- android开发步步为营之84:selector与shape的结合使用
- android开发框架afinal使用方法小结
- [Android开发]Android Notification的使用
- Android开发之使用Android studio结合辅助工具分析内存泄露
- Android开发中Notification的使用
- Android开发之Notification的使用