Android添加顶部通知(Notification)并处于“正在进行中”(onGoing)

用过Android版的QQ的都知道,QQ返回的时候在顶部通知里会出现一个企鹅,表明QQ正在运行,可以拉开通知,点击手机QQ出现主界面,继续运行。

所以我想让自己的软件也出现这么一个通知,并且可以快速的打开查看。

Android应用开发详解 8.3 Notification和NotificationManager的使用 P178

 
/**
* 添加顶部通知
* @author liuzhao
*/
public void AddNotification(){
//添加通知到顶部任务栏
//获得NotificationManager实例
String service = NOTIFICATION_SERVICE;
nm = (NotificationManager)getSystemService(service);
//实例化Notification
n = new Notification();
//设置显示图标
int icon = R.drawable.ic_launcher_home;
//设置提示信息
String tickerText = “我的程序”;
//显示时间
long when = System.currentTimeMillis();

n.icon = icon;
n.tickerText = tickerText;
n.when = when;
//显示在“正在进行中”
n.flags = Notification.FLAG_ONGOING_EVENT;

//实例化Intent
Intent intent = new Intent(tykmAndroid.this,tykmAndroid.class);
//获得PendingIntent
PendingIntent pi = PendingIntent.getActivity(tykmAndroid.this, 0, intent, 0);
//设置事件信息,显示在拉开的里面
n.setLatestEventInfo(tykmAndroid.this, “我的软件”, “我的软件正在运行……”, pi);
//发出通知
nm.notify(ID,n);
}

 

-------------------------------------------------------------------------------------

 

正在进行的和持续的Notification

 

通过设置FLAG_INSISTENTFLAG_ONGOING_EVENT 标志位可以让Notification成为持续或正在进行的Notification

 

Notification标记为ONGOING,如下面的代码所示,它就能用于表示当前正在进行的事件(如来电)。正在进行的事件与“普通的”Notification区别在扩展的状态条窗口中。

 

notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;

 

持续的Notification一直重复,直到用户取消。下面的代码给出了如何设置Notification为持续的:

 

notification.flags = notification.flags | Notification.FLAG_INSISTENT;

 

持续Notification反复重复开头的Notification效果,直到用户取消。持续的Notification应该保留给如闹钟的情形,它需要及时的采取响应。

原文地址:https://www.cnblogs.com/error404/p/2144913.html