项目作者: linglongxin24

项目描述 :
全新的Android通知栏,已抛弃setLatestEventInfo,兼容高版本
高级语言: Java
项目地址: git://github.com/linglongxin24/NotificationUtil.git
创建时间: 2016-11-11T09:21:28Z
项目社区:https://github.com/linglongxin24/NotificationUtil

开源协议:

下载


全新的Android通知栏,已抛弃setLatestEventInfo,兼容高版本

这算是一个入门级的Android通知栏notification的文章,因为在项目中要用到,又发现以前的低版本的用setLatestEventInfo已过时,还报错,完全不兼容。所以,在这里介绍下基本用法,代码比较简单,高手请略过。

先看效果图





1.主要参数介绍

图片1
图片2
图片3

  • 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.显示一个普通的通知栏

  1. /**
  2. * 显示一个普通的通知
  3. *
  4. * @param context 上下文
  5. */
  6. public static void showNotification(Context context) {
  7. Notification notification = new NotificationCompat.Builder(context)
  8. /**设置通知左边的大图标**/
  9. .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
  10. /**设置通知右边的小图标**/
  11. .setSmallIcon(R.mipmap.ic_launcher)
  12. /**通知首次出现在通知栏,带上升动画效果的**/
  13. .setTicker("通知来了")
  14. /**设置通知的标题**/
  15. .setContentTitle("这是一个通知的标题")
  16. /**设置通知的内容**/
  17. .setContentText("这是一个通知的内容这是一个通知的内容")
  18. /**通知产生的时间,会在通知信息里显示**/
  19. .setWhen(System.currentTimeMillis())
  20. /**设置该通知优先级**/
  21. .setPriority(Notification.PRIORITY_DEFAULT)
  22. /**设置这个标志当用户单击面板就可以让通知将自动取消**/
  23. .setAutoCancel(true)
  24. /**设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)**/
  25. .setOngoing(false)
  26. /**向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合:**/
  27. .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
  28. .setContentIntent(PendingIntent.getActivity(context, 1, new Intent(context, MainActivity.class), PendingIntent.FLAG_CANCEL_CURRENT))
  29. .build();
  30. NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
  31. /**发起通知**/
  32. notificationManager.notify(0, notification);
  33. }
  34. `

3.显示一个下载带进度条的通知

  1. /**
  2. * 显示一个下载带进度条的通知
  3. *
  4. * @param context 上下文
  5. */
  6. public static void showNotificationProgress(Context context) {
  7. //进度条通知
  8. final NotificationCompat.Builder builderProgress = new NotificationCompat.Builder(context);
  9. builderProgress.setContentTitle("下载中");
  10. builderProgress.setSmallIcon(R.mipmap.ic_launcher);
  11. builderProgress.setTicker("进度条通知");
  12. builderProgress.setProgress(100, 0, false);
  13. final Notification notification = builderProgress.build();
  14. final NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
  15. //发送一个通知
  16. notificationManager.notify(2, notification);
  17. /**创建一个计时器,模拟下载进度**/
  18. Timer timer = new Timer();
  19. timer.schedule(new TimerTask() {
  20. int progress = 0;
  21. @Override
  22. public void run() {
  23. Log.i("progress", progress + "");
  24. while (progress <= 100) {
  25. progress++;
  26. try {
  27. Thread.sleep(100);
  28. } catch (InterruptedException e) {
  29. // TODO Auto-generated catch block
  30. e.printStackTrace();
  31. }
  32. //更新进度条
  33. builderProgress.setProgress(100, progress, false);
  34. //再次通知
  35. notificationManager.notify(2, builderProgress.build());
  36. }
  37. //计时器退出
  38. this.cancel();
  39. //进度条退出
  40. notificationManager.cancel(2);
  41. return;//结束方法
  42. }
  43. }, 0);
  44. }

4.显示一个悬挂式的通知

  1. /**
  2. * 悬挂式,支持6.0以上系统
  3. *
  4. * @param context
  5. */
  6. public static void showFullScreen(Context context) {
  7. NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
  8. Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/itachi85/"));
  9. PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, mIntent, 0);
  10. builder.setContentIntent(pendingIntent);
  11. builder.setSmallIcon(R.mipmap.ic_launcher);
  12. builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher));
  13. builder.setAutoCancel(true);
  14. builder.setContentTitle("悬挂式通知");
  15. //设置点击跳转
  16. Intent hangIntent = new Intent();
  17. hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  18. hangIntent.setClass(context, MainActivity.class);
  19. //如果描述的PendingIntent已经存在,则在产生新的Intent之前会先取消掉当前的
  20. PendingIntent hangPendingIntent = PendingIntent.getActivity(context, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);
  21. builder.setFullScreenIntent(hangPendingIntent, true);
  22. NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
  23. notificationManager.notify(3, builder.build());
  24. }
  25. `

5.显示一个折叠式的通知

  1. /**
  2. * 折叠式
  3. *
  4. * @param context
  5. */
  6. public static void shwoNotify(Context context) {
  7. //先设定RemoteViews
  8. RemoteViews view_custom = new RemoteViews(context.getPackageName(), R.layout.view_custom);
  9. //设置对应IMAGEVIEW的ID的资源图片
  10. view_custom.setImageViewResource(R.id.custom_icon, R.mipmap.icon);
  11. view_custom.setTextViewText(R.id.tv_custom_title, "今日头条");
  12. view_custom.setTextColor(R.id.tv_custom_title, Color.BLACK);
  13. view_custom.setTextViewText(R.id.tv_custom_content, "金州勇士官方宣布球队已经解雇了主帅马克-杰克逊,随后宣布了最后的结果。");
  14. view_custom.setTextColor(R.id.tv_custom_content, Color.BLACK);
  15. NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
  16. mBuilder.setContent(view_custom)
  17. .setContentIntent(PendingIntent.getActivity(context, 4, new Intent(context, MainActivity.class), PendingIntent.FLAG_CANCEL_CURRENT))
  18. .setWhen(System.currentTimeMillis())// 通知产生的时间,会在通知信息里显示
  19. .setTicker("有新资讯")
  20. .setPriority(Notification.PRIORITY_HIGH)// 设置该通知优先级
  21. .setOngoing(false)//不是正在进行的 true为正在进行 效果和.flag一样
  22. .setSmallIcon(R.mipmap.icon);
  23. Notification notify = mBuilder.build();
  24. NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
  25. notificationManager.notify(4, notify);
  26. }

6.GitHub