Notification

一:普通Notification

 1.内容标题setContentTitle(...)
 2.大图标setLargeIcon(Bitmap)
 3.内容setContentText(...)
 4.内容附加信息setContentInfo(...)
 5.小图标setSmallIcon(...)
 6.时间(自动生成)

二:Notification的创建

1>实例化一个NotificationCompat.Builder对象
2>调用builder的相关方法对notification设置标题,图标,内容等;
3>调用builder.build()方法,此方法返回一个Notification类的对象;
4>实例化一个NotificationManager对象
5>调用manager的notify(...)方法 来发送通知。

三:普通的Notification(点击之后跳到打电话界面,不需要可以不写setContentIntent这个方法)

private NotificationManager notificationManager;
// 获取通知管理器的系统组件实例
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
private void showNormal() {
//建造者模式----利用静态内部类来构建宿主类的对象,链式编程
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("ContentTitle")//设置通知标题
.setContentText("ContentText")//设置通知内容
.setTicker("showNormal")//设置滚动字幕
.setContentInfo("contentInfo")//设置附加信息
.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE)//声音|震动通知,设置震动的时候需要设置权限: <uses-permission android:name="android.permission.VIBRATE" />
//DEFAULT_ALL:声音,震动,呼吸灯都有
.setContentIntent(PendingIntent.getActivity(this, 100,//100是requestCode
new Intent(Intent.ACTION_CALL,
Uri.parse("tel:10086")),
PendingIntent.FLAG_ONE_SHOT))//需要添加打电话权限,否则不会报错但无效果(延时意图不能在logCat中查看),如果没设置延时意图, 则点击通知无法自动消除.
.build(); //构建Notification 类对象
//注意:是notification对象的flags,不是Builder对象的
notification.flags |= Notification.FLAG_AUTO_CANCEL; //设置为点击后可清除,否则点击无法清除[必须同时设置了延时意图PendingIntent]
notification.flags |= Notification.FLAG_NO_CLEAR; //设置为不能横向滑动清除,否则横向拖动可清除,等价于builder对象调用.setOngoing(true)
notificationManager.notify(1, notification); //指定id,发送通知
}

四:带进度条的Notification

 private void showProgressBar() {
//final:因为要在内部类里面使用builder .
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("showProgressBar").setContentInfo("contentInfo")
.setOngoing(true) //不可拖动清除
.setContentTitle("ContentTitle")
.setContentText("ContentText");
//启动一个线程来更新进度,因为不能阻塞UI线程
new Thread(new Runnable() {//Thread构造方法的实参是一个匿名内部类(实现了Runnable接口)
@Override
public void run() {
for (int progress = 0; progress < 100; progress += 10) {
//第一个参数 最大进度, 第二个参数 当前进度
//第三个参数 无明确进度的进度条样式; true:不确定;false:确定
builder.setProgress(100, progress, false);
notificationManager.notify(2, builder.build());//发送通知
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("sleep failure");
}
}
builder.setContentTitle("下载完成")
.setProgress(0, 0, false).setOngoing(true);//设置为下载完成后可清除
notificationManager.notify(2, builder.build());
}
}).start();
}

五:返回应用的某个界面[了解]

有时候我们可能需要实现这样的功能:当新notification出现时,我们希望点击它后可直接进入应用相应的界面中去完整查看或处理此消息的功能。 然后,当我们点击back按钮时返回到应用主界面而不是桌面。比如:当我们有新的短信来时,我们在任务栏中点击它后进入读信息页面,当我们读完短信后,按“返回”键回到短信的主界面,而不是手机桌面。要实现这样的功能要我们做相应的处理:
private void backToApp() {
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(OtherActivity.class);
// Adds the Intent to the top of the stack
Intent resultIntent = new Intent(this, OtherActivity.class);//点击通知后会打开OtherActivity,回退后显示MainActivity,再回退后退出app
//并在清单文件中配置OtherActivity依赖的父窗体为MainActivity(详见下面xml配置)
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,//int requestCode
PendingIntent.FLAG_UPDATE_CURRENT);//如果系统中有一个和你描述的PendingIntent对等的PendingInent,那么系统将使用该PendingIntent对象,但是会使用新 的Intent来更新之前PendingIntent中的Intent对象数据,例如更新Intent中的Extras。

Notification notification = new NotificationCompat.Builder(this)
.setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
.setTicker("backApp").setContentInfo("contentInfo")
.setContentTitle("ContentTitle").setContentText("ContentText")
.setContentIntent(resultPendingIntent).setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL).build();
manager.notify(5, notification);
this.finish();//关闭当前Activity
}

并需要我们在配置文件中对我们用来显示详细信息的OtherActivity进行相应的配置如下:

<activity
android:name=".OtherActivity"
android:label="第二个页面"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>

原文地址:https://www.cnblogs.com/niupi/p/5519709.html