Android Notification 基础
来源:互联网 发布:自定义表情制作软件 编辑:程序博客网 时间:2024/06/11 07:57
在Android中,基本的Notification就是有事件发生的时候在屏幕顶端的Notificat
我们先看一下Notification的Sample Code,然后逐行做解说,大致能了解它的基本构成。
import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;...private void showNotification(Message msg, int id) {NotificationManager notiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);Notification notification = new Notification(R.drawable.notiicon, msg.getTitle(), System.currentTimeMillis());notification.flags = Notification.FLAG_AUTO_CANCEL;Intent intent = new Intent(this, MainActivity.class);Bundle bundle = new Bundle();bundle.putString("info", msg.getInfo());intent.putExtras(bundle);intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NEW_TASK);PendingIntent contentIntent = PendingIntent.getActivity(this, id,intent, PendingIntent.FLAG_UPDATE_CURRENT);notification.setLatestEventInfo(this, msg.getTitle(), msg.getInfo(),contentIntent);notiManager.notify(id, notification);}
首先导入三个类,Notification,NotificationManager,PendingIn
NotificationManager notiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
上面这一句,从系统中获得Notification服务,getSystemService()就是这么用
Notification notification = new Notification(R.drawable.notiicon, msg.getTitle(), System.currentTimeMillis());notification.flags = Notification.FLAG_AUTO_CANCEL;
然后是构造一个Notification,包括三个属性,图标,图标后面的文字,以及Notificati
Intent intent = new Intent(this, MainActivity.class);Bundle bundle = new Bundle();bundle.putString("info", msg.getInfo());intent.putExtras(bundle);intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NEW_TASK);
上面这部分Code,构造一个Intent,并且放进去一条信息。 FLAG_ACTIVITY_CLEAR_TOP, FLAG_ACTIVITY_NEW_TASK者两个FLAG表示优先寻找已经打开的应用,如果应用没有打
PendingIntent contentIntent = PendingIntent.getActivity(this, id,intent, PendingIntent.FLAG_UPDATE_CURRENT);
这一句代码把Intent包装在PendingIntent里,this是Context,id是Pend
notification.setLatestEventInfo(this, msg.getTitle(), msg.getInfo(),contentIntent);notiManager.notify(id, notification);
最后这两行添加状态栏的详细信息,包装PendingIntent给Notification,最后发送N
- .Android Notification 基础
- Android Notification 基础
- Android之基础Notification
- Android基础-Notification用法
- Android 基础:Notification
- Android-notification-基础详解
- Android--Notification基础介绍
- Android Notification 基础(一)
- Android Notification 基础 (二)
- Android Notification 基础 (三)
- android基础笔记:notification通知
- Android 基础 notification 简单应用
- Android基础_通知(Notification)
- android基础学习(9)-------notification提醒
- Android Notification 使用详解之一:基础应用
- Android之Status bar notification基础学习
- Android基础——简易的Notification
- Android基础学习之Notification(通知)
- [摘]Eclipse中用空格(space)代替制表符(tab)的设置
- Linux下C/C++编译器gcc使用简介
- C语言:浮点数在内存中的表示
- 我的一天我做主(Enjoy)- 7
- strust2.xml详解
- Android Notification 基础
- Java中的ReentrantLock和synchronized两种锁定机制的对比
- android按键映射
- linux 查看文件内容的命令
- 深入浅出学枚举
- Android Binder机制の代码角度分析
- HashSet和HashCode的关系
- 查看服务器硬盘信息
- Android的UI构造试图工具hierarchyviewer