Notification的简单应用
使通知显示在顶部,并跳转
public void send(View view) {
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("标题");
builder.setContentText("内容");
builder.setWhen(System.currentTimeMillis());
builder.setAutoCancel(true);
builder.setPriority(Notification.PRIORITY_MAX);
builder.setDefaults(Notification.DEFAULT_ALL);
Intent intent=new Intent(this,Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
builder.setContentIntent(pendingIntent);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
Notification build = builder.build();
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(1,build);
}
使用自定义布局通知
public void send2(View view) {
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("通知头");
builder.setContentText("内容");
builder.setAutoCancel(true);
builder.setPriority(Notification.PRIORITY_MAX);
builder.setDefaults(Notification.DEFAULT_ALL);
Intent intent=new Intent(this,Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notifity);
builder.setCustomContentView(remoteViews);
Notification build = builder.build();
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(2,build);
}
进度条通知
public void send4(View view) {
final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
final Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("和平精英");
builder.setContentText("正在下载...");
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int index=0;
@Override
public void run() {
index+=10;
builder.setProgress(100,index,false);
manager.notify(2, builder.build());
if (index>100){
builder.setContentText("正在安装...");
builder.setProgress(100,index,true);
manager.notify(2, builder.build());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
manager.cancel(2);
timer.cancel();
}
}
},0,1000);
Notification build = builder.build();
manager.notify(2,build);
}
通知分组
public void send6(View view) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(this);
builder.setGroup("2");
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("1111");
builder.setContentText("2222");
Notification.Builder builder2 = new Notification.Builder(this);
builder2.setGroup("2");
builder2.setSmallIcon(R.mipmap.ic_launcher);
builder2.setContentTitle("333");
builder2.setContentText("2444222");
Notification.Builder builder3 = new Notification.Builder(this);
builder3.setGroup("2");
builder3.setSmallIcon(R.mipmap.ic_launcher);
builder3.setContentTitle("1155511");
builder3.setContentText("2266622");
manager.notify(1,builder.build());
manager.notify(2,builder2.build());
manager.notify(3,builder3.build());
}
展示大图及多行文字通知
public void send5(View view) {
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("通知头");
builder.setContentText("内容");
Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
bigPictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
builder.setStyle(bigPictureStyle);
Notification build = builder.build();
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(3,build);
}