Notification 带进度条& 自定义contentView

版权声明:本文为博主原创文章,转载希望能注明出处,感谢。 https://blog.csdn.net/u010126792/article/details/83750179

Notification作用是告诉用户App的通知,主要作用是展示信息&跳转界面(打开界面,服务,通知)&展示后台正在进行的任务进度。举例:微信消息,新闻推送,音乐播放进度,文件下载进度。

1 Notification 创建

主要涉及到 Notification.Builder 、 Notification 、 NotificationManager 。

  • Notification.Builer : 使用建造者模式构建 Notification 对象。
  • Notification : 通知对应类,保存通知相关的数据。
  • NotificationManager : NotificationManager 是通知管理类,它是一个系统服务。调用 NotificationManager 的 notify() 方法可以向系统发送通知。
    private Notification notification;
    NotificationCompat.Builder builder;
    NotificationManager notificationManager;
 builder = new NotificationCompat.Builder(context);
        builder.setContentTitle("正在更新...") //设置通知标题
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher_round)) //设置通知的大图标
                .setDefaults(Notification.DEFAULT_LIGHTS) //设置通知的提醒方式: 呼吸灯
 //震动、响铃、呼吸灯等多种方式进行提醒
                .setPriority(NotificationCompat.PRIORITY_MAX) //设置通知的优先级:最大
                .setAutoCancel(false)//设置通知被点击一次是否自动取消
                .setContentText("下载进度:" + "0%")
                .setProgress(100, 0, false);
         notification = builder.build();//构建通知对象

        builder.setProgress(100, (int) (50), false);
        builder.setContentText("下载进度:" + (int) (50) + "%");
        notification = builder.build();
        notificationManager.notify(1, notification);

进度设置
 builder.setProgress(100, (int) (50), false);
        builder.setContentText("下载进度:" + (int) (50) + "%");
        notification = builder.build();
        notificationManager.notify(1, notification);

2添加事件

 builder.setContentTitle("下载完成")
                            .setContentText("点击安装")
                            .setAutoCancel(true);//设置通知被点击一次是否自动取消 //点击安装代码块
//                    builder.setProgress(0, 0, true);
                    builder.setProgress(0, 0, false);
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    File file = new File("");
                    intent.setDataAndType(Uri.parse("file://" + file.toString()), "application/vnd.android.package-archive");
                    PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
                    notification = builder.setContentIntent(pi).build();
                    notificationManager.notify(1, notification);
                    PendingIntent pi = 

PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
最后一个参数为flag一般为0,
flags的常见类型有:

FLAG_ONE_SHOT:只能被使用一次,然后就会被自动cancel,如果后续还有相同的PendingIntent。那么他们的send方法就会调用失败。
FLAG_NO_CREATE:如果当前系统中不存在相同的PendingIntent对象,系统不会创建该PendingIntent对象,而是直接返回null。(很少使用)

FLAG_CANCEL_CURRENT:如果当前系统中已经存在一个相同的 PendingIntent 对象,那么就将先将已有的 PendingIntent 取消,然后重新生成一个 PendingIntent 对象。

FLAG_UPDATE_CURRENT:当前描述的PendingIntent如果已经存在,那么它们会被更新,即Intent中的Extras会被替换到最新的。

3设置远程RemoteView

   notification = builder.build();//构建通知对象

        RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.activity_main);

        remoteViews.setTextViewText(R.id.idtv,"第一条通知");

       // builder.setProgress(100, (int) (50), false);
       // builder.setContentText("下载进度:" + (int) (50) + "%");
        builder.setContent(remoteViews);
        notification = builder.build();

        notificationManager.notify(1, notification);

模拟器上显示有问题,真机显示没问题。

在这里插入图片描述

4 悬挂通知

public void xuanguaNotification() {

        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification.Builder builderd=new Notification.Builder(this);
        Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
        PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);
        builderd.setContentIntent(pendingIntent);
        builderd.setSmallIcon(R.mipmap.ic_launcher);
        builderd.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
        builderd.setAutoCancel(true);
        builderd.setContentTitle("悬挂通知");

        Intent xuangua=new Intent();
        xuangua.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        xuangua.setClass(this,MainActivity.class);

        PendingIntent xuanpengdIntent=PendingIntent.getActivity(this,0,xuangua,PendingIntent.FLAG_CANCEL_CURRENT);
        builderd.setFullScreenIntent(xuanpengdIntent,true);
        mNotificationManager.notify(2,builderd.build());

    }

有的真机上无法显示,只能当做普通通知,模拟器上可以完美显示,慎用。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/u010126792/article/details/83750179