欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android notification 的总结分析

程序员文章站 2023-12-10 22:24:28
分类  notification有以下几种:   1>普通notification        1.内容标题     2.大图标     3....

分类

 notification有以下几种:

  1>普通notification

  android notification 的总结分析

    1.内容标题

    2.大图标

    3.内容

    4.内容附加信息

    5.小图标

    6.时间

  2>大布局notification

    android notification 的总结分析图1

  大布局notification是在android4.1以后才增加的,大布局notification与小布局notification只在‘7'部分有区别,其它部分都一致。大布局notification只有在所有notification的最上  面时才会显示大布局,其它情况下显示小布局。你也可以用手指将其扩展为大布局(前提是它是大布局)。如下图:

  android notification 的总结分析图2

    大布局notification有三种类型:如图1为notificationcompat.inboxstyle 类型。图2左部为notificationcompat.bigtextstyle。图2右部 为:notificationcompat.bigpicturestyle

   3>自定义布局notification

     除了系统提供的notification,我们也可以自定义notification。如下图所示的一个音乐播放器控制notification:

    android notification 的总结分析图3

如何创建notification


    1>实例化一个notificationcompat.builder对象;如builder

     2>调用builder的相关方法对notification进行上面提到的各种设置

    3>调用builder.build()方法此方法返回一个notification对象。

     4>实例化一个notificationmanager对象;如:manager

     5>调用manager的notify方法。

  注:

   一个notification不必对上面所有的选项都进行设置,但有3项是必须的:

   小图标, set by setsmallicon()

     内容标题, set by setcontenttitle()

     内容, set by setcontenttext()

示例代码



示例程序截图:

 android notification 的总结分析

0>初始化部分代码

复制代码 代码如下:

view code
 public class mainactivity extends activity implements onclicklistener {

     private static final int notification_id_1 = 0;
     private static final int notification_id_2 = 1;
     private static final int notification_id_3 = 2;
     private static final int notification_id_4 = 3;
     private static final int notification_id_5 = 4;
     private static final int notification_id_6 = 5;
     private static final int notification_id_7 = 6;
     private static final int notification_id_8 = 7;

     private static int messagenum = 0;
     private context context = this;
     private notificationmanager manager;
     private bitmap icon;
     private static final int[] btns = new int[] { r.id.btn1, r.id.btn2,
             r.id.btn3, r.id.btn4, r.id.btn5, r.id.btn6, r.id.btn7, r.id.btn8,
             r.id.btn9 };

     @override
     protected void oncreate(bundle savedinstancestate) {
         super.oncreate(savedinstancestate);
         setcontentview(r.layout.activity_main);
         init();
     }

     private void init() {
         // 获取通知服务
         manager = (notificationmanager) getsystemservice(context.notification_service);
         // 注册监听器
         for (int btn : btns) {
             findviewbyid(btn).setonclicklistener(this);
         }

         icon = bitmapfactory.decoderesource(getresources(),
                 r.drawable.ic_launcher);
     }

     @override
     public void onclick(view v) {
         switch (v.getid()) {
         case r.id.btn1:
             shownormal();
             break;
         case r.id.btn2:
             showbigview_text();
             break;
         case r.id.btn3:
             showbigview_pic();
             break;
         case r.id.btn4:
             showbigview_inbox();
             break;
         case r.id.btn5:
             showcustomview();
             break;
         case r.id.btn6:
             backapp();
             break;
         case r.id.btn7:
             backscreen();
             break;
         case r.id.btn8:
             showprogressbar();
             break;
         case r.id.btn9:
             dismiss();
             break;
         default:
             toast.maketext(context, "error", toast.length_short).show();
             break;
         }
     }

     private void dismiss() {
         manager.cancelall();
     }

1>普通notification
复制代码 代码如下:

view code
     private void shownormal() {

         notification notification = new notificationcompat.builder(context)
                 .setlargeicon(icon).setsmallicon(r.drawable.ic_launcher)
                 .setticker("shownormal").setcontentinfo("contentinfo")
                 .setcontenttitle("contenttitle").setcontenttext("contenttext")
                 .setnumber(++messagenum)
                 .setautocancel(true).setdefaults(notification.default_all)
                 .build();
         manager.notify(notification_id_1, notification);
     }

2>大布局text类型notification
复制代码 代码如下:

view code
 private void showbigview_text() {
         notificationcompat.bigtextstyle textstyle = new bigtextstyle();
         textstyle
                 .setbigcontenttitle("bigcontenttitle")
                 .setsummarytext("summarytext")
                 .bigtext(
                         "i am big texttttttttttttttttttttttttttttttttttttttttttt!!!!!!!!!!!!!!!!!!!......");
         notification notification = new notificationcompat.builder(context)
                 .setlargeicon(icon).setsmallicon(r.drawable.ic_launcher)
                 .setticker("showbigview_text").setcontentinfo("contentinfo")
                 .setcontenttitle("contenttitle").setcontenttext("contenttext")
                 .setstyle(textstyle)
                 .setautocancel(true).setdefaults(notification.default_all)
                 .build();
         manager.notify(notification_id_2, notification);
     }

3> 大布局picture类型notificatio
复制代码 代码如下:

view code
 private void showbigview_pic() {
         notificationcompat.bigpicturestyle picturestyle = new bigpicturestyle();
         picturestyle.setbigcontenttitle("bigcontenttitle")
                 .setsummarytext("summarytext").bigpicture(icon);
         notification notification = new notificationcompat.builder(context)
                 .setlargeicon(icon).setsmallicon(r.drawable.ic_launcher)
                 .setticker("showbigview_pic").setcontentinfo("contentinfo")
                 .setcontenttitle("contenttitle").setcontenttext("contenttext")
                 .setstyle(picturestyle)
                 .setautocancel(true).setdefaults(notification.default_all)
                 .build();
         manager.notify(notification_id_3, notification);
     }

4>大布局inbox类型notification
复制代码 代码如下:

view code
 private void showbigview_inbox() {
         notificationcompat.inboxstyle inboxstyle = new notificationcompat.inboxstyle();
         inboxstyle.setbigcontenttitle("bigcontenttitle").setsummarytext(
                 "summarytext");
         for (int i = 0; i < 5; i++)
             inboxstyle.addline("news:" + i);
         notification notification = new notificationcompat.builder(context)
                 .setlargeicon(icon).setsmallicon(r.drawable.ic_launcher)
                 .setticker("showbigview_inbox").setcontentinfo("contentinfo")
                 .setcontenttitle("contenttitle").setcontenttext("contenttext")
                 .setstyle(inboxstyle)
                 .setautocancel(true).setdefaults(notification.default_all)
                 .build();
         manager.notify(notification_id_4, notification);
     }

5>自定义notification
效果图:

android notification 的总结分析

并对中间的播放按钮做了一个简单的点击处理事件(点击播放后,请关闭幕帘否则可能会看不到toast提示)

复制代码 代码如下:

view code
 private void showcustomview() {
         remoteviews remoteviews = new remoteviews(getpackagename(),
                 r.layout.custom_notification);
         intent intent = new intent(this, testmusiccontrol.class);
         pendingintent pendingintent = pendingintent.getbroadcast(context, 0,
                 intent, 0);
         remoteviews.setonclickpendingintent(r.id.paly_pause_music,
                 pendingintent);
         notificationcompat.builder builder = new builder(context);
         builder.setcontent(remoteviews).setsmallicon(r.drawable.music_icon)
                 .setlargeicon(icon).setongoing(true)
                 .setticker("music is playing");
         manager.notify(notification_id_8, builder.build());
     }

布局文件:
复制代码 代码如下:

view code
 <?xml version="1.0" encoding="utf-8"?>
 <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:gravity="center_vertical"
     android:orientation="horizontal" >

     <imageview
         android:id="@+id/songer_pic"
         android:layout_width="64dp"
         android:layout_height="64dp"
         android:src="@drawable/yan" />

     <linearlayout
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:gravity="center_vertical"
         android:orientation="horizontal" >

         <imageview
             android:id="@+id/last_music"
             android:layout_width="0dp"
             android:layout_height="48dp"
             android:layout_weight="1"
             android:src="@drawable/music_previous" />

         <imageview
             android:id="@+id/paly_pause_music"
             android:layout_width="0dp"
             android:layout_height="48dp"
             android:layout_weight="1"
             android:src="@drawable/music_play" />

         <imageview
             android:id="@+id/next_music"
             android:layout_width="0dp"
             android:layout_height="48dp"
             android:layout_weight="1"
             android:src="@drawable/music_next" />
     </linearlayout>

 </linearlayout>

带进度条的notification
复制代码 代码如下:

view code
     private void showprogressbar() {

         final notificationcompat.builder builder = new notificationcompat.builder(
                 context);
         builder.setlargeicon(icon).setsmallicon(r.drawable.ic_launcher)
                 .setticker("showprogressbar").setcontentinfo("contentinfo")
                 .setongoing(true).setcontenttitle("contenttitle")
                 .setcontenttext("contenttext");
         new thread(new runnable() {

             @override
             public void run() {

                 int progress = 0;

                 for (progress = 0; progress < 100; progress += 5) {
                     //将setprogress的第三个参数设为true即可显示为无明确进度的进度条样式
                     builder.setprogress(100, progress, false);
                     manager.notify(notification_id_7, builder.build());
                     try {
                         // sleep for 5 seconds
                         thread.sleep(2 * 1000);
                     } catch (interruptedexception e) {
                         system.out.println("sleep failure");
                     }
                 }
                 builder.setcontenttitle("download complete")
                         .setprogress(0, 0, false).setongoing(false);
                 manager.notify(notification_id_7, builder.build());

             }
         }).start();
     }

点击事件处理

--------------------------------------------------------------------------------
  有时候我们可能需要实现这样的功能:当新notification出现时,我们希望点击它后可直接进入应用相应的界面中去完整查看或处理此消息的功能。然后,当我们点击back按钮时返回到应用主界面而不是桌面。比如:当我们有新的短信来时,我们在任务栏中点击它后进入读信息页面,当我们读完短信后,按“返回”键回到短信的主界面,而不是手机桌面。要实现这样的功能要我们做相应的处理:

1>返回应用主界面

复制代码 代码如下:

view code
     private void backapp() {

         taskstackbuilder stackbuilder = taskstackbuilder.create(this);
         // adds the back stack
         stackbuilder.addparentstack(otheractivity.class);
         // adds the intent to the top of the stack
         intent resultintent = new intent(this, otheractivity.class);
         stackbuilder.addnextintent(resultintent);
         // gets a pendingintent containing the entire back stack
         pendingintent resultpendingintent = stackbuilder.getpendingintent(0,
                 pendingintent.flag_update_current);

         notification notification = new notificationcompat.builder(context)
                 .setlargeicon(icon).setsmallicon(r.drawable.ic_launcher)
                 .setticker("backapp").setcontentinfo("contentinfo")
                 .setcontenttitle("contenttitle").setcontenttext("contenttext")
                 .setcontentintent(resultpendingintent).setautocancel(true)
                 .setdefaults(notification.default_all).build();
         manager.notify(notification_id_5, notification);
         this.finish();
     }

并需要我们在配置文件中对我们用来显示详细信息的otheractivity进行相应的配置如下:
复制代码 代码如下:

<activity
             android:name="com.example.notification.otheractivity"
             android:label="@string/title_activity_other"
             android:parentactivityname=".mainactivity" >
             <meta-data
                 android:name="android.support.parent_activity"
                 android:value=".mainactivity" />
         </activity>

2>直接返回桌面

   有些时候我们可能需要实现这样的功能:当我们点击notification时弹出一个稍大点的窗口来显示整个消息,这窗口的作用就是用来显示整个消息内容的,和此应用内的其它activity都没有关系,然后当我们点击"back"后直接返回到手机桌面。要实现这样的功能我们只需要调用builder的.setcontentintent方法,然后对所要跳转到的activity在配置文件中进行一些配置:

复制代码 代码如下:

view code
 private void backscreen() {
         intent notifyintent = new intent(this, specialactivity.class);
         // sets the activity to start in a new, empty task
         notifyintent.setflags(intent.flag_activity_new_task
                 | intent.flag_activity_clear_task);
         // creates the pendingintent
         pendingintent notify_intent = pendingintent.getactivity(this, 0,
                 notifyintent, pendingintent.flag_update_current);

         notification notification = new notificationcompat.builder(context)
                 .setlargeicon(icon).setsmallicon(r.drawable.ic_launcher)
                 .setticker("backscreen").setcontentinfo("contentinfo")
                 .setcontenttitle("contenttitle").setcontenttext("contenttext")
                 .setcontentintent(notify_intent).setautocancel(true)
                 .setdefaults(notification.default_all).build();
         manager.notify(notification_id_6, notification);
         this.finish();
     }

配置文件:
复制代码 代码如下:

 <activity
             android:name="com.example.notification.specialactivity"
             android:excludefromrecents="true"
             android:label="@string/title_activity_special"
             android:launchmode="singletask"
             android:taskaffinity="" >

源码下载