通知栏

通知栏

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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值