通知
进度条通知
public void one(View view) {
final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
final Notification.Builder builder = new Notification.Builder(Main2Activity.this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("标题");
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int index=0;
@Override
public void run() {
builder.setProgress(100,index,false);
index+=10;
manager.notify(1,builder.build());
if (index>=100){
builder.setContentTitle("正在安装");
builder.setProgress(0,0,true);
manager.notify(1,builder.build());
try {
Thread.sleep(5000);
timer.cancel();
manager.cancel(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
},0,1000);
manager.notify(1,builder.build());
}
通知分组
public void two(View view) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder1 = new Notification.Builder(Main2Activity.this);
Notification.Builder builder2 = new Notification.Builder(Main2Activity.this);
Notification.Builder builder3 = new Notification.Builder(Main2Activity.this);
Notification.Builder builder4 = new Notification.Builder(Main2Activity.this);
builder1.setGroup("2");
builder1.setGroupSummary(true);
builder1.setContentTitle("标题1");
builder1.setSmallIcon(R.mipmap.ic_launcher);
builder1.setContentText("1111");
builder2.setGroup("2");
builder2.setContentTitle("标题2");
builder2.setSmallIcon(R.mipmap.ic_launcher);
builder2.setContentText("2222");
builder3.setGroup("2");
builder3.setContentTitle("标题3");
builder3.setSmallIcon(R.mipmap.ic_launcher);
builder3.setContentText("3333");
builder4.setGroup("2");
builder4.setContentTitle("标题4");
builder4.setSmallIcon(R.mipmap.ic_launcher);
builder4.setContentText("4444");
manager.notify(1,builder1.build());
manager.notify(2,builder2.build());
manager.notify(3,builder3.build());
manager.notify(4,builder4.build());
}
自定义通知
public void three(View view) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(Main2Activity.this);
builder.setSmallIcon(R.mipmap.ic_launcher);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.my_layout);
builder.setCustomContentView(remoteViews);
manager.notify(1,builder.build());
}
修改自定义内容
public void four(View view) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(Main2Activity.this);
builder.setSmallIcon(R.mipmap.ic_launcher);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.my_layout);
remoteViews.setTextViewText(R.id.aa,"马保国");
remoteViews.setTextViewText(R.id.bb,"年轻人不讲武德");
remoteViews.setImageViewResource(R.id.img,R.mipmap.aa);
builder.setCustomContentView(remoteViews);
manager.notify(1,builder.build());
}
修改自定义内容
public void five(View view) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(Main2Activity.this);
builder.setSmallIcon(R.mipmap.aa);
builder.setContentTitle("马保国");
builder.setContentText("年轻人不讲武德");
builder.setPriority(Notification.PRIORITY_MAX);
builder.setDefaults(Notification.DEFAULT_ALL);
builder.setVisibility(Notification.VISIBILITY_SECRET);
builder.setAutoCancel(true);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.my_layout);
builder.setCustomContentView(remoteViews);
Intent intent = new Intent(Main2Activity.this, MainActivity.class);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentIntent(activity);
manager.notify(1,builder.build());
}