通知

通知

进度条通知

//进度条通知
    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());

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值