通知是(Notification)是Android系统中的特色功能。当程序小玩那个想用户发出提示信息,二程序又不在前台运行时,就可以借助通知实现。通知的用法比较灵活,既可以在活动中创建,也可以在广播接收者,服务里创建。相比于广播接收器和服务,活动中创建的情况较少
通知的种类
普通通知,进度条通知,自定义通知,分组通知,锁屏通知
通知的实现流程:
获取通知管理器,实例化通知,设置通知属性,通知管理器发送通知
具体代码
普通通知
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public void normal(View view) {
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("普通通知");
builder.setContentText("我是普通通知");
builder.setAutoCancel(true);//点击后自动消失
Intent intent1 = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,101,intent1,PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
Notification notification = builder.build();
notificationManager.notify(1,notification);
}
分组通知
NotificationManager notificationManager;
public void group(View view) {
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("普通通知");
builder.setContentText("我是普通通知");
builder.setGroup("a");
builder.setGroupSummary(true);//将这条通知折叠到最后一层
Notification.Builder builder1 = new Notification.Builder(this);
builder1.setSmallIcon(R.mipmap.ic_launcher);
builder1.setContentTitle("普通通知1");
builder1.setContentText("我是普通通知1");
builder1.setGroup("a");
Notification.Builder builder2 = new Notification.Builder(this);
builder2.setSmallIcon(R.mipmap.ic_launcher);
builder2.setContentTitle("普通通知2");
builder2.setContentText("我是普通通知2");
builder2.setGroup("a");
Notification notification = builder.build();
Notification notification1=builder1.build();
Notification notification2=builder2.build();
notificationManager.notify(2,notification);
notificationManager.notify(3,notification1);
notificationManager.notify(4,notification2);
}
进度条通知
Timer timer;
public void progress(View view) {
timer = new Timer();
final Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("正在下载。。。");
builder.setProgress(100,0,false);
notificationManager.notify(5,builder.build());
timer.schedule(new TimerTask() {
int progress=0;
@Override
public void run() {
if (progress == 100) {
notificationManager.cancel(5);
timer.cancel();
}else {
progress+=20;
builder.setProgress(100,progress,false);
notificationManager.notify(5,builder.build());
}
}
},0,1000);
}
自定义通知
public void diy(View view) {
Notification.Builder builder = new Notification.Builder(this);
RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.diy);
remoteViews.setTextViewText(R.id.diy_tv,"知行合一");
builder.setSmallIcon(R.mipmap.ic_launcher)
.setCustomContentView(remoteViews);
Notification notification = builder.build();
notificationManager.notify(6,notification);
NotificationTarget target = new NotificationTarget(MainActivity.this,R.id.diy_iv,remoteViews,notification,6);
Glide.with(this).asBitmap().load("https://img2.baidu.com/it/u=3202947311,1179654885&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500").into(target);
}
<ImageView
android:id="@+id/diy_iv"
android:layout_width="100dp"
android:layout_height="100dp"></ImageView>
<TextView
android:id="@+id/diy_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>