android开发(11) 消息栏通知(Notification)
来源:互联网 发布:一针见血网络电影 编辑:程序博客网 时间:2024/06/15 06:08
演示截图:
实现步骤:
1.获得NotificationManager 对象,这是一个通知管理器。我们在窗体里调用方法获得
NotificationManager manager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE);
getSystemService是获得系统服务的方法。android以服务的形式提供给用户操作接口。也就是说,我们要想操作 通知相关的操作接口,就先获得系统提供的 “通知管理器”
NotificationManager 对象就是一个服务管理器了。
2.构建一个Notification 对象,这个Notification 对象描述了:通知的标题和内容,通知要调用的窗体。
//构建一个通知对象,指定了 图标,标题,和时间
Notification notification = new Notification(R.drawable.icon, "通知", System.currentTimeMillis());//设定事件信息
notification.setLatestEventInfo(getApplicationContext(),
"通知标题",
"通知显示的内容",
pendingIntent);notification.flags|=Notification.FLAG_AUTO_CANCEL; //自动终止
notification.defaults |= Notification.DEFAULT_SOUND; //默认声音
其中pendingIntent对象 是一个跳转intent,当提示后,点击在消息提示栏的 “通知”时,能打开一个窗体activity
PendingIntent pendingIntent = PendingIntent.getActivity(
ActNotifyDemo.this,
0,
new Intent(ActNotifyDemo.this,ActNotifyDemo.class), //指定一个跳转的intent
0
);实际上是PendingIntent 包含(封装)了一个跳转的 intent对象。
3.调用NotificationManager.notify方法发起通知,发起后的通知就会在消息栏提示。
-----------------
代码如下:
Button _btn1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_btn1.setOnClickListener(new OnClickListener(){
//触发通知
public void onClick(View arg0) {
//获得通知管理器
NotificationManager manager = (NotificationManager) getSystemService(
Context.NOTIFICATION_SERVICE);
//构建一个通知对象
Notification notification = new Notification(R.drawable.icon,
"通知", System.currentTimeMillis());
PendingIntent pendingIntent = PendingIntent.getActivity(
ActNotifyDemo.this,
0,
new Intent(ActNotifyDemo.this,ActNotifyDemo.class),
0
);
notification.setLatestEventInfo(getApplicationContext(),
"通知标题",
"通知显示的内容",
pendingIntent);
notification.flags|=Notification.FLAG_AUTO_CANCEL; //自动终止
notification.defaults |= Notification.DEFAULT_SOUND; //默认声音
manager.notify(0, notification);//发起通知
}
});
}
}
源代码下载
- android开发(11) 消息栏通知(Notification)
- Android Notification通知栏消息
- android通知栏的消息通知(Notification)
- Android 消息通知-Notification
- Android 消息通知-Notification
- Android调用消息栏通知(Notification)
- Android 中的消息通知Notification
- Android的消息通知--Notification
- Android的消息通知--Notification
- Android开发&短信拦截与读取,消息通知Notification
- Android开发总结笔记 Notification(消息通知) 1-1-20
- Android开发笔记之notification消息推送 通知栏的实现
- Android开发之通知栏Notification详解
- Android Notification通知栏开发详解
- Android Notification通知栏开发详解
- Android Notification通知栏开发详解
- Android开发之Notification 通知栏
- Android开发通知栏Notification详解
- POJ1006: 中国剩余定理的完美演绎(转)
- android开发(9) 渐变动画演示(Tween Animation)
- android:gravity 和 android:layout_Gravity
- android开发(10) 逐帧动画演示(Frame Animation)
- jedis read time out
- android开发(11) 消息栏通知(Notification)
- 收藏站点列表
- winfrom - 重定向控制台的输入输出
- C#中的装箱与拆箱
- 和飞秋的通讯实现
- .net 命名管道(NamedPipe) 的使用
- iOS使用表视图(UITableView)的一些问题
- linbet————udp实例
- 我学正则表达式——组(group)和回溯引用(backreference)