全新的Android通知栏,已抛弃setLatestEventInfo,兼容高版本
16lz
2021-01-23
转载请注明出处:http://blog.csdn.net/linglongxin24/article/details/53166551
本文出自【DylanAndroid的博客】
全新的Android通知栏,已抛弃setLatestEventInfo,兼容高版本
这算是一个入门级的Android通知栏notification的文章,因为在项目中要用到,又发现以前的低版本的用setLatestEventInfo已过时,还报错,完全不兼容。所以,在这里介绍下基本用法,代码比较简单,高手请略过。上图:
1.主要参数介绍
- 1.notification的title
- 2.发送notification的application的icon或者发送者的image。
- 3.notification的message
- 4.notification的数目显示
- 5.当main icon用来显示sender’s image时 Secondary icon用来显示发送application的icon。
- 6.时间戳,默认为系统发出通知的时间。
2.显示一个普通的通知栏
/** * 显示一个普通的通知 * * @param context 上下文 */ public static void showNotification(Context context) { Notification notification = new NotificationCompat.Builder(context) /**设置通知左边的大图标**/ .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher)) /**设置通知右边的小图标**/ .setSmallIcon(R.mipmap.ic_launcher) /**通知首次出现在通知栏,带上升动画效果的**/ .setTicker("通知来了") /**设置通知的标题**/ .setContentTitle("这是一个通知的标题") /**设置通知的内容**/ .setContentText("这是一个通知的内容这是一个通知的内容") /**通知产生的时间,会在通知信息里显示**/ .setWhen(System.currentTimeMillis()) /**设置该通知优先级**/ .setPriority(Notification.PRIORITY_DEFAULT) /**设置这个标志当用户单击面板就可以让通知将自动取消**/ .setAutoCancel(true) /**设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)**/ .setOngoing(false) /**向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合:**/ .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND) .setContentIntent(PendingIntent.getActivity(context, 1, new Intent(context, MainActivity.class), PendingIntent.FLAG_CANCEL_CURRENT)) .build(); NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); /**发起通知**/ notificationManager.notify(0, notification); }
3.显示一个下载带进度条的通知
/** * 显示一个下载带进度条的通知 * * @param context 上下文 */ public static void showNotificationProgress(Context context) { //进度条通知 final NotificationCompat.Builder builderProgress = new NotificationCompat.Builder(context); builderProgress.setContentTitle("下载中"); builderProgress.setSmallIcon(R.mipmap.ic_launcher); builderProgress.setTicker("进度条通知"); builderProgress.setProgress(100, 0, false); final Notification notification = builderProgress.build(); final NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); //发送一个通知 notificationManager.notify(2, notification); /**创建一个计时器,模拟下载进度**/ Timer timer = new Timer(); timer.schedule(new TimerTask() { int progress = 0; @Override public void run() { Log.i("progress", progress + ""); while (progress <= 100) { progress++; try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } //更新进度条 builderProgress.setProgress(100, progress, false); //再次通知 notificationManager.notify(2, builderProgress.build()); } //计时器退出 this.cancel(); //进度条退出 notificationManager.cancel(2); return;//结束方法 } }, 0); }
4.显示一个悬挂式的通知
/** * 悬挂式,部分系统厂商不支持 * * @param context */ public static void showFullScreen(Context context) { NotificationCompat.Builder builder = new NotificationCompat.Builder(context); Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/linglongxin24")); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, mIntent, 0); builder.setContentIntent(pendingIntent); builder.setSmallIcon(R.mipmap.ic_launcher); builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher)); builder.setAutoCancel(true); builder.setContentTitle("悬挂式通知"); //设置点击跳转 Intent hangIntent = new Intent(); hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); hangIntent.setClass(context, MainActivity.class); //如果描述的PendingIntent已经存在,则在产生新的Intent之前会先取消掉当前的 PendingIntent hangPendingIntent = PendingIntent.getActivity(context, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT); builder.setFullScreenIntent(hangPendingIntent, true); NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); notificationManager.notify(3, builder.build()); }
5.显示一个折叠式的通知
/** * 折叠式 * * @param context */ public static void shwoNotify(Context context) { //先设定RemoteViews RemoteViews view_custom = new RemoteViews(context.getPackageName(), R.layout.view_custom); //设置对应IMAGEVIEW的ID的资源图片 view_custom.setImageViewResource(R.id.custom_icon, R.mipmap.icon); view_custom.setTextViewText(R.id.tv_custom_title, "今日头条"); view_custom.setTextColor(R.id.tv_custom_title, Color.BLACK); view_custom.setTextViewText(R.id.tv_custom_content, "金州勇士官方宣布球队已经解雇了主帅马克-杰克逊,随后宣布了最后的结果。"); view_custom.setTextColor(R.id.tv_custom_content, Color.BLACK); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context); mBuilder.setContent(view_custom) .setContentIntent(PendingIntent.getActivity(context, 4, new Intent(context, MainActivity.class), PendingIntent.FLAG_CANCEL_CURRENT)) .setWhen(System.currentTimeMillis())// 通知产生的时间,会在通知信息里显示 .setTicker("有新资讯") .setPriority(Notification.PRIORITY_HIGH)// 设置该通知优先级 .setOngoing(false)//不是正在进行的 true为正在进行 效果和.flag一样 .setSmallIcon(R.mipmap.icon); Notification notify = mBuilder.build(); NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); notificationManager.notify(4, notify); }
6.GitHub
更多相关文章
- android通知栏进度条
- android带有文字的图片按钮的两种实现方式
- android 7和android8在user版本下能应用获得root
- 演化理解 Android 异步加载图片
- Android布局的一些属性和开关、创建log图片
- Android WebView实现选择本地图片拍照功能
- 很实用的android压缩图片的算法
- android imageView 图片显示方式属性
- Android 为联系人添加图片