一:概念
Notification,俗称通知,是一种具有全局效果的通知,它展示在屏幕的顶端,首先会表现为一个图标的形式,当用户向下滑动的时候,展示出通知具体的内容。
二:讲解和实现
1.Notification:在Android4.x中,用Notification.Builder(this).builder()创建,在3.0之前用new Notification()来创建
Notification notify =
new
Notification.Builder(
this
)
.setAutoCancel(
true
)
.setTicker(
"有新消息"
)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(
"新通知"
)
.setContentText(
"加薪了"
)
.setWhen(System.currentTimeMillis())
.setContentIntent(PendingIntent);
.build();
2.NotificationManager:是一个通知管理器类,这个对象是由系统维护的服务,是以单例模式的方式获得,所以一般并不直接实例化这个对象。
在Activity中,可以使用Activity.getSystemService(String)方法获取NotificationManager对象,Activity.getSystemService(String)方法可以通过Android系统级服务的句柄,返回对应的对象。在这里需要返回NotificationManager,所以直接传递Context.NOTIFICATION_SERVICE即可。
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
3.PendingIntent:要得到一个pendingIntent对象,使用方法类的静态方法
getActivity(Context, int, Intent, int)
,getBroadcast(Context, int, Intent, int)
,
getService(Context, int, Intent, int) 分别对应着Intent的3个行为,跳转到一个activity组件、打开一个广播组件和打开一个服务组件。
Context:上下文,一般为当前对象,this
Intent:就是一个Intent
最后一个参数:
FLAG_CANCEL_CURRENT:如果构建的PendingIntent已经存在,则取消前一个,重新构建一个。
FLAG_NO_CREATE:如果前一个PendingIntent已经不存在了,将不再构建它。
FLAG_ONE_SHOT:表明这里构建的PendingIntent只能使用一次。
FLAG_UPDATE_CURRENT:如果构建的PendingIntent已经存在,则替换它,常用。
实现:
StatusService.java
package com.jiajia.statusbar;
import android.annotation.SuppressLint;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
public class StatusService extends IntentService {
public StatusService() {
super("StatusService");
}
@Override
protected void onHandleIntent(Intent intent) {
System.out.println("开始下载。。。");
showNotification(false);
try{
Thread.sleep(10000);
showNotification(true);
}catch(Exception e){
e.printStackTrace();
}
}
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
private void showNotification(boolean f){
@SuppressWarnings("deprecation")
Intent intent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification;
/**
* 4.x用这个
*/
// notification = new Notification.Builder(this).build();
// .setAutoCancel(true)
// .setTicker("有新消息")
// .setSmallIcon(R.drawable.ic_launcher)
// .setContentTitle("新通知")
// .setContentText("加薪了")
// .setWhen(System.currentTimeMillis())
// .setContentIntent(pi)
if(f == false){
/**
* new Notification() 已过时
*/
notification = new Notification(R.drawable.xiazai, "开始下载", System.currentTimeMillis());
notification.setLatestEventInfo(this, "下载", "正在下载中", contentIntent);
}else{
notification = new Notification(R.drawable.xiazai, "下载完成", System.currentTimeMillis());
notification.setLatestEventInfo(this, "下载完成", "完成下载", contentIntent);
}
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(R.id.home, notification);
}
}
补:
1:notification中when干吗的
System.currentTimeMillis(); 获取当前的毫秒数 可以用来测试程序的运行时间 读取数据的时候来计算时间的! 通知栏 设置 提示的间隔时间 ==s 通知栏的三个参数中,图标内容,和发生时间。 Notification(icon, tickerText,when); 如果同一个通知栏有新的内容要更新根据这个时间来判断更新。
持续更新。。。