在Android中使用Notification进行提示

jopen 12年前
     <p>用惯了<span>Android</span>的人在刚拿到iPhone的时候,总是会习惯性的用手指从<span>状态栏</span>往下拖一下,这都是给<span>Notification</span>闹的。<br /> 不过<span>Notification</span>也确实是1个不错的<span>提示</span>工具,不干扰正常的操作,事后还可以再翻看详细的内容,点击后还可以进入相关的画面查看更具体的内容。<br /> 今天我就以代码为主的形式来介绍<span>Notification</span>的使用,包括基本用法,<span>自定义</span>的View,以及更多的控制方法。<br /> 另一种<span>Android</span>中常用到的<span>提示</span>方法Toast的用法请参见《教程:在Android中使用Toast进行提示》<br /> 我们先看下<span>Notification</span>的几个主要组成部分:<br /> Icon:不解释<br /> Ticker Text:Notification刚出来的时候,在<span>状态栏</span>上滚动的字幕,如果很长,会自动分割滚动<br /> <img title="Icon和Ticker Text" border="0" alt="Icon和Ticker Text" src="https://simg.open-open.com/show/e09fe8c989b1db5190c38482d94c48d3.jpg" width="323" height="63" /><br /> Content Title:Notification展开后的标题<br /> Content Text:Notification展开后的内容<br /> <img title="Content Title和Text" border="0" alt="Content Title和Text" src="https://simg.open-open.com/show/e09fe8c989b1db5190c38482d94c48d3.jpg" width="323" height="63" /></p>    <p></p>    <h3>Notification的一般用法</h3>    <p>取得NotificationManager</p>    <pre class="brush:java; toolbar: true; auto-links: false;">private NotificationManager mNotificationManager; mNotificationManager = (NotificationManager)   getSystemService(Context.NOTIFICATION_SERVICE);</pre>    <p></p> 创建Notification并且显示    <pre class="brush:java; toolbar: true; auto-links: false;">//Notification的滚动提示 String tickerText = "My notification, It's a long text! Hello World desiyo?"; //Notification的图标,一般不要用彩色的 int icon = R.drawable.icon_02241_3;   //contentTitle和contentText都是标准的Notification View的内容 //Notification的内容标题,拖下来后看到的标题 String contentTitle="My notification"; //Notification的内容 String contentText="Hello World!";   //Notification的Intent,即点击后转向的Activity Intent notificationIntent = new Intent(this, this.getClass()); notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent contentIntent = PendingIntent.getActivity(this, 0,   notificationIntent, 0);   //创建Notifcation Notification notification = new Notification(icon, tickerText, System.currentTimeMillis()); //设定Notification出现时的声音,一般不建议自定义 notification.defaults |= Notification.DEFAULT_SOUND; //设定如何振动 notification.defaults |= Notification.DEFAULT_VIBRATE; //指定Flag,Notification.FLAG_AUTO_CANCEL意指点击这个Notification后,立刻取消自身 //这符合一般的Notification的运作规范 notification.flags|=Notification.FLAG_AUTO_CANCEL; notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent); //显示这个notification mNotificationManager.notify(HELLO_ID, notification);</pre>    <p></p>    <p>这是最基本的应用,可以说除了找个合适的图标以外,其它都很简单。</p>    <h3>使用<span>自定义</span>View的Notification</h3>    <p>同Toast一样,我们也可以自已指定1个View来作为Notification展开后的显示内容,比如说在<span>Android</span> Market中下载的时候,Notification中会显示当前下载的进度,那么我们也来模拟1个这样的效果吧。<br /> 首先给出View的定义文件:notification_view_sample.xml</p>    <pre class="brush:xml; toolbar: true; auto-links: false;"><?xml version="1.0" encoding="utf-8"?>  <RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:padding="3dp" >  <ImageView android:id="@+id/notificationImage"    android:layout_width="wrap_content" android:layout_height="wrap_content"   android:src="@android:drawable/stat_sys_download"  />  <TextView android:id="@+id/notificationTitle"    android:layout_width="wrap_content" android:layout_height="wrap_content"   android:layout_toRightOf="@id/notificationImage"   android:layout_alignParentRight="true"   android:paddingLeft="6dp"    android:textColor="#FF000000"  />  <TextView android:id="@+id/notificationPercent"   android:layout_width="wrap_content" android:layout_height="wrap_content"    android:layout_below="@id/notificationImage"   android:paddingTop="2dp"   android:textColor="#FF000000"  />  <ProgressBar android:id="@+id/notificationProgress"    android:layout_width="wrap_content" android:layout_height="wrap_content"   android:layout_below="@id/notificationTitle"   android:layout_alignLeft="@id/notificationTitle"   android:layout_alignParentRight="true"    android:layout_alignTop="@id/notificationPercent"   android:paddingLeft="6dp"   android:paddingRight="3dp"   android:paddingTop="2dp"   style="?android:attr/progressBarStyleHorizontal"   /> </RelativeLayout></pre>    <p></p> 接下来是Java代码片段:    <pre class="brush:java; toolbar: true; auto-links: false;">//Notification的滚动提示 String tickerText1 = "Custom view for download notification"; //Notification的图标,一般不要用彩色的 int icon1 = android.R.drawable.stat_sys_download;   //Notification的Intent,即点击后转向的Activity Intent notificationIntent1 = new Intent(this, this.getClass()); notificationIntent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent contentIntent1 = PendingIntent.getActivity(this, 0, notificationIntent1, 0);   //创建Notifcation Notification notification1 = new Notification(icon1, tickerText1, System.currentTimeMillis()); //设定Notification出现时的声音,一般不建议自定义 notification1.defaults |= Notification.DEFAULT_SOUND; //设定是否振动 notification1.defaults |= Notification.DEFAULT_VIBRATE; //notification.number=numbers++; //指定Flag,Notification.FLAG_AUTO_CANCEL意指点击这个Notification后,立刻取消自身 //这符合一般的Notification的运作规范 notification1.flags|=Notification.FLAG_ONGOING_EVENT;   //创建RemoteViews用在Notification中 RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_view_sample); contentView.setTextViewText(R.id.notificationTitle, "Download:非死book for android"); contentView.setTextViewText(R.id.notificationPercent, "35%"); contentView.setProgressBar(R.id.notificationProgress, 100, 35, false);   notification1.contentView = contentView; notification1.contentIntent=contentIntent1;   //显示这个notification mNotificationManager.notify(CUSTOM_VIEW_ID, notification1);</pre>    <p></p>    <p>注意以上代码中使用的是RemoteViews,而不是普通的View,另外使用的是PendingIntent而不是普通的Intent,这都说明了Notification是1个“远程”的东西,其中能够使用的控件是受限制的,比如说TableLayout就不能使用。看下效果图,是不是和 Market中的界面很接近呢?<br /> <img title="自定义View的Notification效果图" border="0" alt="自定义View的Notification效果图" src="https://simg.open-open.com/show/c064f187fa8982a758547977ceed34d2.jpg" width="319" height="90" /></p>    <h3>更好的控制Notification</h3>    <h5>动画图标怎么做?</h5>    <p>和selector类似,定义1个XML文件放在drawable下,下面是之前用到的stat_sys_download的定义:</p>    <pre class="brush:xml; toolbar: true; auto-links: false;"><animation-list         xmlns:android="http://schemas.android.com/apk/res/android"         android:oneshot="false">     <item android:drawable="@drawable/stat_sys_download_anim0" android:duration="200" />      <item android:drawable="@drawable/stat_sys_download_anim1" android:duration="200" />     <item android:drawable="@drawable/stat_sys_download_anim2" android:duration="200" />      <item android:drawable="@drawable/stat_sys_download_anim3" android:duration="200" />     <item android:drawable="@drawable/stat_sys_download_anim4" android:duration="200" />      <item android:drawable="@drawable/stat_sys_download_anim5" android:duration="200" /> </animation-list></pre>    <p></p>    <h5>如何更新Notification?</h5>    <p>注意到前面的代码中用到的CUSTOM_VIEW_ID,这是Notification的ID,如果2次弹出的Notification的ID相同,那么Notification就只会更新而不会再次滚动提醒。之前给出的ProgressBar是不会动的,利用这个方法就可以让它动起来(或者也可以直接调用RemoteView的set方法来直接更新?未试验)</p>    <h5>如何<span>自定义</span><span>提示</span>的声音和振动?</h5>    <pre>//自定义提示音 notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3"); //自定义振动方式 long[] vibrate = {0,100,200,300}; notification.vibrate = vibrate;</pre>    <p>请注意:如果使用了DEFAULT_SOUND或DEFAULT_VIBRATE,则自定义的<span>提示</span>音和振动无效。</p>    <h5>在类似于短消息的应用中如何<span>提示</span>数量?</h5>    <p>使用Notification的number属性,默认为0,如果是1或更大的数字,则会在图标上覆盖显示这个数字。<br /> notification.number=notificationNumber;</p>    <h5>Flag的使用</h5>    <p>notification有1个flag属性,除了DEFAULT_SOUND之外,还有几个很有用的属性。<br /> FLAG_AUTO_CANCEL:自动清除Notification,前面的例子中有用到<br /> FLAG_INSISTENT:提示音一直不停,直至用户响应(很吵吧!)<br /> FLAG_ONGOING_EVENT:表示这是1个正在进行的任务,不可以清除,第2个例子中有用到<br /> FLAG_NO_CLEAR:不可以清除</p>    <p>文章出处:<a href="/misc/goto?guid=4959499960401948338" rel="nofollow">http://www.learningandroid.net/blog/others/tutorial-android-notification-sample/</a></p>