资讯专栏INFORMATION COLUMN

Notification 使用详解

sorra / 3480人阅读

摘要:简介通知在用户界面的一个重要部分,其使用方法请看以下内容继承关系如下简介通知是应用向用户显示的消息提示,当发送通知时,通知将先以图标的形式显示在通知区域中。通知区域和下拉通知栏均是由系统控制的区域,用户可以随时查看。

极力推荐文章:欢迎收藏
Android 干货分享

阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android

本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以下内容:

</>复制代码

  1. Notification 简介

  2. 通知的创建

  3. 通知的管理

  4. 简单的通知

  5. 可以 扩展的通知

  6. 通知中含下载进度条

  7. 通知中含媒体播放控件

  8. 自定义通知内容

Notification 通知是应用向用户显示的消息提示,当发送通知时,通知将先以图标的形式显示在通知区域中。用户可以打开下拉通知栏查看通知的详细信息。 通知区域和下拉通知栏均是由系统控制的区域,用户可以随时查看。

Notification 简介

通知在Android用户界面的一个重要部分,其使用方法请看以下内容:

Notification 继承关系如下:

</>复制代码

  1. java.lang.Object
  2.    ↳
  3. android.app.Notification
1.Notification 简介

通知是应用向用户显示的消息提示,当发送通知时,通知将先以图标的形式显示在通知区域中。用户可以打开下拉通知栏查看通知的详细信息。 通知区域和下拉通知栏均是由系统控制的区域,用户可以随时查看。

2.创建Notification 的方法

调用NotificationCompat.Builder.build() 创建Notification 对象

然后调用 NotificationManager.notify() Notification 对象传递给系统。

Notification 对象必须包含以下内容:

小图标,由 setSmallIcon() 设置

标题,由 setContentTitle() 设置

详细文本,由 setContentText() 设置

通知可选内容

设置优先级

通知默认优先级为 PRIORITY_DEFAULT 0
Notification.Builder.setPriority()
5个级别可选(-2、-1、0、1、2)

通知优先级如下:

</>复制代码

  1. PRIORITY_LOW=-1
  2. PRIORITY_MIN=-2
  3. PRIORITY_DEFAULT = 0
  4. PRIORITY_HIGH=1
  5. PRIORITY_MAX=2
设置可以扩展样式

通过Notification.Builder.setStyle()可以设置通知的样式。

点击通知启动Activity(PendingIntent)

通知中经常遇到,点击通知栏,打开 Activity

</>复制代码

  1. Notification.Builder mBuilder = new Notification.Builder(this);
  2. mBuilder.setSmallIcon(R.drawable.gril)
  3. .setDefaults(Notification.DEFAULT_SOUND).setColor(000)
  4. .setContentTitle("简单通知Tittle").setContentText("点击可以打开Activity");
  5. Intent resultIntent = new Intent(this, NotificationMethods.class);
  6. // 新开一个Activity 栈
  7. resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
  8. | Intent.FLAG_ACTIVITY_CLEAR_TASK);
  9. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
  10. stackBuilder.addParentStack(NotificationMethods.class);
  11. stackBuilder.addNextIntent(resultIntent);
  12. PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
  13. PendingIntent.FLAG_UPDATE_CURRENT);
  14. mBuilder.setContentIntent(resultPendingIntent);
  15. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  16. mNotificationManager.notify(0, mBuilder.build());
3. 通知的管理

更新通知

调用 NotificationManager.notify(ID) 发出带有通知 ID 的通知,ID相同,即可更新以前ID发送的通知。

删除通知

创建时 调用了 setAutoCancel(true)

删除时候调用删除指定ID

</>复制代码

  1. NotificationManager.cancel(notificationId)

删除自己应用发的所有通知

</>复制代码

  1. Utils.mNotificationManager.cancelAll();

在通知中显示进度条

setProgress()

4. 简单的通知

实现效果

实现代码

</>复制代码

  1. /**
  2. * 简单通知
  3. */
  4. public void SimpleNotification(View view) {
  5. Notification.Builder mBuilder = new Notification.Builder(this);
  6. mBuilder.setSmallIcon(R.drawable.gril)
  7. .setDefaults(Notification.DEFAULT_SOUND).setColor(000)
  8. .setContentTitle("简单通知Tittle").setContentText("点击可以打开Activity");
  9. Intent resultIntent = new Intent(this, NotificationMethods.class);
  10. // 新开一个Activity 栈
  11. resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
  12. | Intent.FLAG_ACTIVITY_CLEAR_TASK);
  13. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
  14. stackBuilder.addParentStack(NotificationMethods.class);
  15. stackBuilder.addNextIntent(resultIntent);
  16. PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
  17. PendingIntent.FLAG_UPDATE_CURRENT);
  18. mBuilder.setContentIntent(resultPendingIntent);
  19. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  20. mNotificationManager.notify(0, mBuilder.build());
  21. }
5. 可以 扩展的通知

实现效果

实现代码

</>复制代码

  1. /**
  2. * 可扩展通知
  3. * **/
  4. public void NotificationStyle(View view) {
  5. Notification.Builder mBuilder = new Notification.Builder(this);
  6. mBuilder.setLargeIcon(
  7. DrawableUtils.DrawableToBitmap(getResources().getDrawable(
  8. R.drawable.ic_launcher)))
  9. .setContentTitle("我是可扩展通知的Tittle ")
  10. .setDefaults(Notification.DEFAULT_SOUND)
  11. .setContentText("我是可扩展通知的内容")
  12. .setSmallIcon(R.drawable.ic_launcher)
  13. .setAutoCancel(true)
  14. .setStyle(
  15. new Notification.InboxStyle().addLine("我是可扩展通知第一行")
  16. .addLine("我是可扩展通知第二行")
  17. .setBigContentTitle("我是可扩展的大 Tittle")
  18. .setSummaryText("点击,展开获取更多内容"));
  19. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  20. // 如果Id 一样可以更新通知
  21. mNotificationManager.notify(1, mBuilder.build());
  22. }
6. 通知中含下载进度条

实现效果

实现代码

</>复制代码

  1. /**
  2. * 带有下载进度条的通知
  3. * **/
  4. public void NotificationProcess(View view) {
  5. final NotificationManager mNotifyManagerProcess;
  6. final Notification.Builder mBuilder;
  7. mNotifyManagerProcess = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  8. mBuilder = new Notification.Builder(this);
  9. mBuilder.setContentTitle("Picture Downloading").setSmallIcon(
  10. R.drawable.ic_launcher);
  11. new Thread(new Runnable() {
  12. @Override
  13. public void run() {
  14. for (MIncr = 0; MIncr <= 100; MIncr += 1 + 5 * Math.random()) {
  15. mBuilder.setProgress(100, MIncr, false).setContentText(
  16. MIncr + "%");
  17. mNotifyManagerProcess.notify(2, mBuilder.build());
  18. try {
  19. Thread.sleep(500);
  20. } catch (InterruptedException e) {
  21. }
  22. }
  23. /**
  24. * setProgress true 则表示 进度条一直不停的从左至右滑动,类似于圆形进度条 false :进度条消失
  25. * **/
  26. mBuilder.setContentText("Download complete").setProgress(0, 0,
  27. false);
  28. mNotifyManagerProcess.notify(2, mBuilder.build());
  29. }
  30. }).start();
  31. }
7. 通知中含媒体播放控件

实现效果

实现代码

</>复制代码

  1. /**
  2. * 音乐播放器样式
  3. * **/
  4. public void NotificationMediaStyle(View view) {
  5. Notification.Builder mMediaBuilder = new Notification.Builder(this);
  6. mMediaBuilder.setSmallIcon(R.drawable.ic_launcher);
  7. mMediaBuilder.setContentTitle("如果有一天我变有钱");
  8. mMediaBuilder.setContentText("毛不易");
  9. mMediaBuilder.setLargeIcon(DrawableUtils
  10. .DrawableToBitmap(getResources().getDrawable(
  11. R.drawable.ic_launcher)));
  12. Intent mIntent = new Intent();
  13. ComponentName name = new ComponentName(this, NotificationMethods.class);
  14. mIntent.setComponent(name);
  15. PendingIntent mPendingIntent = PendingIntent.getActivity(
  16. getApplicationContext(), 0, mIntent, 0);
  17. mMediaBuilder.setContentIntent(mPendingIntent);
  18. mMediaBuilder.setPriority(Notification.PRIORITY_MAX);
  19. mMediaBuilder.addAction(new Notification.Action.Builder(Icon
  20. .createWithResource(NotificationMethods.this,
  21. R.drawable.music_pre), "1", null).build());
  22. mMediaBuilder.addAction(new Notification.Action.Builder(Icon
  23. .createWithResource(NotificationMethods.this,
  24. R.drawable.music_play), "2", null).build());
  25. mMediaBuilder.addAction(new Notification.Action.Builder(Icon
  26. .createWithResource(NotificationMethods.this,
  27. R.drawable.music_next), "3", null).build());
  28. Notification.MediaStyle mMediaStyle = new Notification.MediaStyle();
  29. mMediaStyle.setShowActionsInCompactView(0, 1, 2);
  30. mMediaBuilder.setStyle(mMediaStyle);
  31. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  32. // 如果Id 一样可以更新通知
  33. mNotificationManager.notify(1, mMediaBuilder.build());
  34. }
8. 自定义通知内容

实现效果

实现代码

</>复制代码

  1. /**
  2. * 自定义样式通知
  3. * **/
  4. public void NotificationCustomView(View view) {
  5. /***
  6. * 自定义Remoteview
  7. * **/
  8. RemoteViews remoteViews = new RemoteViews(getPackageName(),
  9. R.layout.notification_view);
  10. remoteViews.setTextViewText(R.id.tv_content_title, "十年");
  11. remoteViews.setTextViewText(R.id.tv_content_text, "陈奕迅");
  12. // 打开上一首
  13. remoteViews.setOnClickPendingIntent(R.id.btn_pre,
  14. SetClickPendingIntent(NOTIFICATION_PRE));
  15. // 打开下一首
  16. remoteViews.setOnClickPendingIntent(R.id.btn_next,
  17. SetClickPendingIntent(NOTIFICATION_NEXT));
  18. // 点击整体布局时,打开播放器
  19. remoteViews.setOnClickPendingIntent(R.id.btn_play,
  20. SetClickPendingIntent(NOTIFICATION_PLAY));
  21. // 点击整体布局时,打开Activity
  22. remoteViews.setOnClickPendingIntent(R.id.ll_root,
  23. SetClickPendingIntent(NOTIFICATION_ACTIVITY));
  24. remoteViews.setOnClickPendingIntent(R.id.img_clear,
  25. SetClickPendingIntent(NOTIFICATION_CANCEL));
  26. Notification.Builder builder = new Notification.Builder(this)
  27. .setSmallIcon(R.drawable.ic_launcher)
  28. .setTicker("当前正在播放..")
  29. .setWhen(System.currentTimeMillis())
  30. .setContentTitle("十年")
  31. .setContentText("陈奕迅")
  32. .setAutoCancel(true)
  33. .setLargeIcon(
  34. DrawableUtils.DrawableToBitmap(getResources()
  35. .getDrawable(R.drawable.ic_launcher)))
  36. .setContent(remoteViews);
  37. Utils.mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  38. // 打开通知
  39. Utils.mNotificationManager.notify(Utils.NOTIFICATION_CUSTOM_ID,
  40. builder.build());
  41. }
  42. public PendingIntent SetClickPendingIntent(int what) {
  43. switch (what) {
  44. case NOTIFICATION_PRE:
  45. Intent intentPre = new Intent(this, MainActivity.class);
  46. intentPre.putExtra("cmd", what);
  47. int flagPre = PendingIntent.FLAG_UPDATE_CURRENT;
  48. PendingIntent clickPreIntent = PendingIntent.getActivity(this,
  49. what, intentPre, flagPre);
  50. return clickPreIntent;
  51. case NOTIFICATION_PLAY:
  52. Intent intentPlay = new Intent(this, NotificationMethods.class);
  53. intentPlay.putExtra("cmd", what);
  54. int flagPlay = PendingIntent.FLAG_UPDATE_CURRENT;
  55. PendingIntent clickPlayIntent = PendingIntent.getActivity(this,
  56. what, intentPlay, flagPlay);
  57. return clickPlayIntent;
  58. case NOTIFICATION_NEXT:
  59. Intent intentNext = new Intent(this, ActivityMethods.class);
  60. intentNext.putExtra("cmd", what);
  61. int flagNext = PendingIntent.FLAG_UPDATE_CURRENT;
  62. PendingIntent clickNextIntent = PendingIntent.getActivity(this,
  63. what, intentNext, flagNext);
  64. return clickNextIntent;
  65. case NOTIFICATION_ACTIVITY:
  66. Intent intentActivity = new Intent(this, ServiceMethod.class);
  67. intentActivity.putExtra("cmd", what);
  68. int flag = PendingIntent.FLAG_UPDATE_CURRENT;
  69. PendingIntent clickIntent = PendingIntent.getActivity(this, what,
  70. intentActivity, flag);
  71. Toast.makeText(getApplicationContext(), "打开Activity", 0).show();
  72. return clickIntent;
  73. case NOTIFICATION_CANCEL:
  74. Intent intentCancel = new Intent("Notification_cancel");
  75. intentCancel.putExtra("cancel_notification_id",
  76. Utils.NOTIFICATION_CUSTOM_ID);
  77. int flagCancel = PendingIntent.FLAG_CANCEL_CURRENT;
  78. PendingIntent clickCancelIntent = PendingIntent.getBroadcast(this,
  79. 0, intentCancel, flagCancel);
  80. return clickCancelIntent;
  81. default:
  82. break;
  83. }
  84. return null;
  85. }

自定View 布局如下:

</>复制代码

实现自定义通知删除按钮事件实现

</>复制代码

  1. case NOTIFICATION_CANCEL:
  2. Intent intentCancel = new Intent("Notification_cancel");
  3. intentCancel.putExtra("cancel_notification_id",
  4. Utils.NOTIFICATION_CUSTOM_ID);
  5. int flagCancel = PendingIntent.FLAG_CANCEL_CURRENT;
  6. PendingIntent clickCancelIntent = PendingIntent.getBroadcast(this,
  7. 0, intentCancel, flagCancel);
  8. return clickCancelIntent;
注意

广播是四大组件之一,需要在AndroidManfest.xml 中注册

注册方式如下:

</>复制代码

至此,本篇已结束,如有不对的地方,欢迎您的建议与指正。同时期待您的关注,感谢您的阅读,谢谢!

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/75842.html

相关文章

  • Service 使用详解

    摘要:只能执行单一操作,无法返回结果给调用方,常用于网络下载上传文件,播放音乐等。绑定模式此模式通过绑定组件等调用启动此服务随绑定组件的消亡而解除绑定。 showImg(https://segmentfault.com/img/remote/1460000019975019?w=157&h=54); 极力推荐文章:欢迎收藏Android 干货分享 showImg(https://segme...

    freewolf 评论0 收藏0
  • Broadcast 使用详解

    摘要:静态注册广播的方法动态注册广播在中动态注册广播,通常格式如下动态注册广播动态注册监听灭屏点亮屏幕的广播在广播中动态注册广播请注意一定要使用,防止为空,引起空指针异常。绑定模式此模式通过绑定组件等调用启动此服务随绑定组件的消亡而解除绑定。 showImg(https://segmentfault.com/img/remote/1460000019975019?w=157&h=54); 极...

    Y3G 评论0 收藏0
  • Android四大组件之Service全解析

    摘要:四大组件都支持这个属性。到目前为止,中总共有三种启动方式。返回值方法有一个的返回值,这个返回值标识服务关闭后系统的后续操作。,启动后的服务被杀死,不能保证系统一定会重新创建。 1. 简介 这篇文章会从Service的一些小知识点,延伸到Android中几种常用进程间通信方法。 2. 进程        Service是一种不提供用户交互页面但是可以在后台长时间运行的组件,可以通过在An...

    alaege 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<