android的通知栏——Notification
2015-09-22 08:53
393 查看
首先是关于通知栏的定义或者说是介绍:
位于手机状态栏的通知,在显示的网络信息和电量信息的下面,反映了全局效果,在任何一个应用中都能够看到通知信息。
其次是如何发送notificantion:
用NotificationManager来发送Notification。
最后是发送通知的步骤:
1.通过调用getSystemServices()的方法来获取系统的Notificationmanager的服务。NotificationManager
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
2.通过构造器创建一个Notification.Builder的对象。
3.实现builder中相关的方法
4.调用builder的build()或者getNotification()方法来获取notification的对象
5.通过Notificationmanager发送Notification;
接下来上主要代码:
private void send() {
//写出即将跳转组件的意图
Intent intent = new Intent(MainActivity.this,Demo02.class);
//封装一个Pending对象,用来封装将要发生的事情
PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this,0,intent, 0);
Notification.Builder builder = new Notification.Builder(MainActivity.this);
builder.setAutoCancel(true); //设置通知栏点击后就消失
builder.setTicker("您有新消息了"); //设置提示信息
builder.setSmallIcon(R.drawable.ic_launcher); //设置通知的图标
builder.setContentTitle("有一条新消息"); //设置通知标题
builder.setContentText("恭喜您,中了500万的现金大奖,点开即可查看详情,并领取奖金哦~"); //设置通知的内容
builder.setDefaults(Notification.DEFAULT_ALL); //设置声音,振动以及led灯等操作
builder.setWhen(System.currentTimeMillis()); //设置发送时间
builder.setContentIntent(pIntent); //设置将要发生的控件的意图
Notification notification = builder.getNotification();//获取通知对象, 方法过时
nm.notify(NOYIFICATION_ID, notification); //发送通知
}
private void cancel(){
nm.cancel(NOYIFICATION_ID); //取消
}
每行的代码解释都很详细,就不多做介绍了。
点击第一个按钮,会弹出一个通知,提醒你有新消息;而点击第二个按钮,则会取消之前发送的通知
下拉状态栏,会看到你自己编辑的那条信息:
点击通知栏上的信息,会进入到你自己遍历的另一个界面,里面正常来说是关于通知的具体消息:
位于手机状态栏的通知,在显示的网络信息和电量信息的下面,反映了全局效果,在任何一个应用中都能够看到通知信息。
其次是如何发送notificantion:
用NotificationManager来发送Notification。
最后是发送通知的步骤:
1.通过调用getSystemServices()的方法来获取系统的Notificationmanager的服务。NotificationManager
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
2.通过构造器创建一个Notification.Builder的对象。
3.实现builder中相关的方法
4.调用builder的build()或者getNotification()方法来获取notification的对象
5.通过Notificationmanager发送Notification;
接下来上主要代码:
private void send() {
//写出即将跳转组件的意图
Intent intent = new Intent(MainActivity.this,Demo02.class);
//封装一个Pending对象,用来封装将要发生的事情
PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this,0,intent, 0);
Notification.Builder builder = new Notification.Builder(MainActivity.this);
builder.setAutoCancel(true); //设置通知栏点击后就消失
builder.setTicker("您有新消息了"); //设置提示信息
builder.setSmallIcon(R.drawable.ic_launcher); //设置通知的图标
builder.setContentTitle("有一条新消息"); //设置通知标题
builder.setContentText("恭喜您,中了500万的现金大奖,点开即可查看详情,并领取奖金哦~"); //设置通知的内容
builder.setDefaults(Notification.DEFAULT_ALL); //设置声音,振动以及led灯等操作
builder.setWhen(System.currentTimeMillis()); //设置发送时间
builder.setContentIntent(pIntent); //设置将要发生的控件的意图
Notification notification = builder.getNotification();//获取通知对象, 方法过时
nm.notify(NOYIFICATION_ID, notification); //发送通知
}
private void cancel(){
nm.cancel(NOYIFICATION_ID); //取消
}
每行的代码解释都很详细,就不多做介绍了。
点击第一个按钮,会弹出一个通知,提醒你有新消息;而点击第二个按钮,则会取消之前发送的通知
下拉状态栏,会看到你自己编辑的那条信息:
点击通知栏上的信息,会进入到你自己遍历的另一个界面,里面正常来说是关于通知的具体消息:
相关文章推荐
- Android基础复习之Spinner
- Android Studio与Android SDK 在线更新的解决方案(1.3.2)
- android的多种对话框
- Android开发总结笔记 FrameLayout(帧布局) 1-1-3
- 半年之内拿下android
- android适配问题
- 源码分析Mms--AndroidManifest.xml分析
- android 画画
- 蜂窝教育Android培训:高薪等于开心吗?
- databinding学习(4)
- Getting Started: Configuring Mac for Android
- Android Activity生命周期详解
- AndroidStudio出现找不到JVM问题
- Android中Manifest的Application的name和getApplication()的关系
- Android studio中几个插件的使用
- Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏
- Android之JSON处理器FastJson
- Android之日期时间选择器使用方法
- [坑]android ListView 加载数据乱码
- Android获取手机及路由器的Mac地址和IP地址