Notitfcation通知

通知是(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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值