打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
android NotificationCompat.Builder 使用

1.首先,获取系统的通知服务:

 

NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
2.发送一个最简单的通知

 

 

  1.     public void simpleNotice(View view) {
  2.         //此Builder为android.support.v4.app.NotificationCompat.Builder中的,下同。
  3.         Builder mBuilder = new Builder(this);
  4. //系统收到通知时,通知栏上面显示的文字。
  5. mBuilder.setTicker(天津,晴,2~15度,微风);
  6. //显示在通知栏上的小图标
  7. mBuilder.setSmallIcon(R.drawable.consult_answer);
  8. //通知标题
  9. mBuilder.setContentTitle(天气预报);
  10. //通知内容
  11. mBuilder.setContentText(天津,晴,2~15度,微风);
  12. //设置大图标,即通知条上左侧的图片(如果只设置了小图标,则此处会显示小图标)
  13. mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.share_sina));
  14. //显示在小图标左侧的数字
  15. mBuilder.setNumber(6);
  16. //设置为不可清除模式
  17. mBuilder.setOngoing(true);
  18. //显示通知,id必须不重复,否则新的通知会覆盖旧的通知(利用这一特性,可以对通知进行更新)
  19. nm.notify(1, mBuilder.build());
  20. }
3.删除一个通知。参数即为通知的id
nm.cancel(1);
4.发送一个通知,点击通知后跳转到一个Activity,从这个Activity返回后,进入程序内的某一个页面(一般为主页)

 

 

  1. //点击通知进入一个Activity,点击返回时进入指定页面。
  2. public void resultActivityBackApp(View view) {
  3. Builder mBuilder = new Builder(this);
  4. mBuilder.setTicker(通知标题2);
  5. mBuilder.setSmallIcon(R.drawable.ic_launcher);
  6. mBuilder.setContentTitle(通知标题2);
  7. mBuilder.setContentText(点击通知进入一个Activity,点击返回时进入指定页面。);
  8. //设置点击一次后消失(如果没有点击事件,则该方法无效。)
  9. mBuilder.setAutoCancel(true);
  10. //点击通知之后需要跳转的页面
  11. Intent resultIntent = new Intent(this, ResultActivityBackApp.class);
  12. //使用TaskStackBuilder为“通知页面”设置返回关系
  13. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
  14. //为点击通知后打开的页面设定 返回 页面。(在manifest中指定)
  15. stackBuilder.addParentStack(ResultActivityBackApp.class);
  16. stackBuilder.addNextIntent(resultIntent);
  17. PendingIntent pIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
  18. mBuilder.setContentIntent(pIntent);
  19. // mId allows you to update the notification later on.
  20. nm.notify(2, mBuilder.build());
  21. }
同时,需要在manifest中为点击通知后打开的Activity指定父Activity.

 

 

(其中,activity的属性parentActivityName为API 16中的属性,meta-data中的代码为兼容API 16以下。因此,对于大多数程序,这两个地方都得写。)

 

5.和上述4类似,只是在打开的Activity中返回时回到home页

 

  1. //点击通知进入一个Activity,点击返回时回到桌面
  2. public void resultActivityBackHome(View view) {
  3. Builder mBuilder = new Builder(this);
  4. mBuilder.setTicker(通知标题3);
  5. mBuilder.setSmallIcon(R.drawable.ic_launcher);
  6. mBuilder.setContentTitle(通知标题3);
  7. mBuilder.setContentText(点击通知进入一个Activity,点击返回时回到桌面);
  8. //设置点击一次后消失(如果没有点击事件,则该方法无效。)
  9. mBuilder.setAutoCancel(true);
  10. Intent notifyIntent = new Intent(this, ResultActivityBackHome.class);
  11. notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  12. PendingIntent pIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  13. mBuilder.setContentIntent(pIntent);
  14. nm.notify(3, mBuilder.build());
  15. }
6.带进度条的通知

 

 

  1.      public void progressNotice(View view) {
  2. final Builder mBuilder = new Builder(this);
  3. mBuilder.setTicker(通知标题4);
  4. mBuilder.setContentTitle(Picture Download)
  5. .setContentText(Download in progress)
  6. .setSmallIcon(R.drawable.ic_launcher);
  7. // Start a lengthy operation in a background thread
  8. new Thread(new Runnable() {
  9. @Override
  10. public void run() {
  11. int progress;
  12. for (progress = 0; progress <= 100; progress++) {
  13. // Sets the progress indicator to a max value, the current completion percentage,
  14. // and determinate state
  15. mBuilder.setProgress(100, progress, false);
  16. //不明确进度的进度条
  17. // mBuilder.setProgress(0, 0, true);
  18. nm.notify(4, mBuilder.build());
  19. // 模拟延时
  20. try {
  21. Thread.sleep(200);
  22. } catch (InterruptedException e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. // When the loop is finished, updates the notification
  27. mBuilder.setContentText(Download complete);
  28. // Removes the progress bar
  29. mBuilder.setProgress(0, 0, false);
  30. nm.notify(4, mBuilder.build());
  31. }
  32. }
  33. ).start();
  34. }
7.扩展布局的通知。按住通知条下滑,可以查看更详细的内容

 

 

  1. public void expandLayoutNotice(View view) {
  2. Builder mBuilder = new Builder(this);
  3. mBuilder.setTicker(通知标题5);
  4. mBuilder.setSmallIcon(R.drawable.ic_launcher);
  5. mBuilder.setContentTitle(通知标题5);
  6. mBuilder.setContentText(按住通知下拉可显示扩展布局);
  7. NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
  8. String[] events = new String[]{Beijing, Tianjin, Shanghai, Guangzhou};
  9. // 设置扩展布局的标题
  10. inboxStyle.setBigContentTitle(Event tracker details:);
  11. for (String s : events) {
  12. inboxStyle.addLine(s);
  13. }
  14. mBuilder.setStyle(inboxStyle);
  15. nm.notify(5, mBuilder.build());
  16. }

8.自定义布局的通知栏。(根据谷歌的官方文档不推荐这么做,因为使用这种方式时,对不同屏幕进行适配需要考虑的因素太多。而且,通知栏应该展示的就是最简明扼要的信息,对于大多数程序默认的布局已经足够了。)

 

 

  1. //自定义布局的通知
  2. public void customLayoutNotice(View view) {
  3. Builder mBuilder = new Builder(this);
  4. mBuilder.setTicker(通知标题6);
  5. mBuilder.setTicker(通知标题6);
  6. mBuilder.setSmallIcon(R.drawable.ic_launcher);
  7. RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.custom_layout_notice);
  8. mBuilder.setContent(remoteViews);
  9. //为RemoteViews上的按钮设置文字
  10. remoteViews.setCharSequence(R.id.custom_layout_button1, setText, Button1);
  11. remoteViews.setCharSequence(R.id.custom_layout_button2, setText, Button2);
  12. //为RemoteViews上的按钮设置点击事件
  13. Intent intent1 = new Intent(this, CustomLayoutResultActivity.class);
  14. intent1.putExtra(content, From button1 click!);
  15. PendingIntent pIntentButton1 = PendingIntent.getActivity(this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
  16. remoteViews.setOnClickPendingIntent(R.id.custom_layout_button1, pIntentButton1);
  17. Intent intent2 = new Intent(this, CustomLayoutResultActivity.class);
  18. intent2.putExtra(content, From button2 click!);
  19. PendingIntent pIntentButton2 = PendingIntent.getActivity(this, 1, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
  20. remoteViews.setOnClickPendingIntent(R.id.custom_layout_button2, pIntentButton2);
  21. nm.notify(6, mBuilder.build());
  22. }
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
承香墨影 Android--通知之Notification
全面了解Android Notification
Android中通知的使用
2.5.8 Notification(状态栏通知)详解 | 菜鸟教程
通知未被驳回(Android)
Android Notification 详解
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服