通知栏
1.普通通知栏
代码
//普通通知
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void bt_custom(){
//TODO 1:通知管理者 --- 老师
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//TODO 2:通知构建者 --- 创建学生的
Notification.Builder builder = new Notification.Builder(this);
//必须设置一个小图标属性
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("我是标题");//设置标题
builder.setContentText("我是内容");
builder.setContentInfo("我是附加信息");
builder.setTicker("我是提示信息");
Intent intent = new Intent(this,Main2Activity.class);
//将intent专程PendingIntent
PendingIntent pendingIntent = PendingIntent.getActivity(this,101,intent,PendingIntent.FLAG_ONE_SHOT);
//设置通知的来电效果
builder.setDefaults(Notification.DEFAULT_VIBRATE);
builder.setContentIntent(pendingIntent);
//TODO 3: 发送通知 --- 老师指令学生上台
manager.notify(1,builder.build());
}
2.自定义通知栏
代码如下
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void bt_normal(){
//TODO 1:通知管理者: 老师
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//TODO 2:通知构建者---创建学生的
Notification.Builder builder = new Notification.Builder(this);
//必须设置一个小圆标属性
builder.setSmallIcon(R.mipmap.ic_launcher);
//设置自定义布局
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.custom_layout);
builder.setContent(remoteViews);
manager.notify(1,builder.build());
}
3.进度条通知栏
代码如下
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void poasset(){
//TODO 1:通知管理者: 老师
final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//TODO 2:通知构建者---创建学生的
final Notification.Builder builder = new Notification.Builder(this);
//必须设置一个小圆标属性
builder.setSmallIcon(R.mipmap.ic_launcher);
manager.notify(1,builder.build());
Timer timer = new Timer();
timer.schedule(new TimerTask() {
int count =0;
@Override
public void run() {
builder.setProgress(100,count +=20,false);
manager.notify(1,builder.build());
if(count==100){
builder.setProgress(100,count,true);
manager.notify(1,builder.build());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
},0,1000);
}