Notification的功能与用法

来源:互联网 发布:淘宝开店怎么做代理商 编辑:程序博客网 时间:2024/05/20 00:11

转载至:http://blog.csdn.net/dawanganban/article/details/12170321


Notifiaction是显示在手机状态栏的通知——手机状态栏位于手机屏幕的最上方,Notifiaction代表的是一种具有全局效果的通知,程序一般通过NotificationManager服务来发送Notification.

NotificationManager是一个重要的系统服务,该API位于应用程序框架层,应用程序可通过NotificationManager向系统发送全局通知。

Android3.0增加Notification.Builder类,通过该类允许开发者更轻松地创建Notification对象。

下面通过一个实例来说明:

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:gravity="center_horizontal"  
  7.     >  
  8. <Button  
  9.     android:layout_width="wrap_content"   
  10.     android:layout_height="wrap_content"   
  11.     android:text="发送Notification"  
  12.     android:onClick="send"  
  13.     />  
  14. <Button  
  15.     android:layout_width="wrap_content"   
  16.     android:layout_height="wrap_content"   
  17.     android:text="删除Notification"  
  18.     android:onClick="del"  
  19.     />     
  20. </LinearLayout>  

NotificationTest.java

  1. package org.crazyit.ui;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.Notification;  
  5. import android.app.NotificationManager;  
  6. import android.app.PendingIntent;  
  7. import android.content.Intent;  
  8. import android.net.Uri;  
  9. import android.os.Bundle;  
  10. import android.view.View;  
  11.   
  12. public class NotificationTest extends Activity  
  13. {  
  14.     static final int NOTIFICATION_ID = 0x123;  
  15.     NotificationManager nm;  
  16.   
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState)  
  19.     {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.         // 获取系统的NotificationManager服务  
  23.         nm = (NotificationManager)   
  24.             getSystemService(NOTIFICATION_SERVICE);  
  25.     }  
  26.   
  27.     // 为发送通知的按钮的点击事件定义事件处理方法  
  28.     public void send(View source)  
  29.     {  
  30.         // 创建一个启动其他Activity的Intent  
  31.         Intent intent = new Intent(NotificationTest.this  
  32.             , OtherActivity.class);  
  33.         PendingIntent pi = PendingIntent.getActivity(  
  34.             NotificationTest.this0, intent, 0);  
  35.         Notification notify = new Notification.Builder(this)  
  36.             // 设置打开该通知,该通知自动消失  
  37.             .setAutoCancel(true)  
  38.             // 设置显示在状态栏的通知提示信息  
  39.             .setTicker("有新消息")  
  40.             // 设置通知的图标  
  41.             .setSmallIcon(R.drawable.notify)  
  42.             // 设置通知内容的标题  
  43.             .setContentTitle("一条新通知")  
  44.             // 设置通知内容  
  45.             .setContentText("恭喜你,您加薪了,工资增加20%!")  
  46.             // // 设置使用系统默认的声音、默认LED灯  
  47.             // .setDefaults(Notification.DEFAULT_SOUND  
  48.             // |Notification.DEFAULT_LIGHTS)  
  49.             // 设置通知的自定义声音  
  50.             .setSound(Uri.parse("android.resource://org.crazyit.ui/"  
  51.                 + R.raw.msg))  
  52.             .setWhen(System.currentTimeMillis())  
  53.             // 设改通知将要启动程序的Intent  
  54.             .setContentIntent(pi).build();  
  55.         // 发送通知  
  56.         nm.notify(NOTIFICATION_ID, notify);  
  57.     }  
  58.   
  59.     // 为删除通知的按钮的点击事件定义事件处理方法  
  60.     public void del(View v)  
  61.     {  
  62.         // 取消通知  
  63.         nm.cancel(NOTIFICATION_ID);  
  64.     }  
  65. }  

运行结果:


0 0
原创粉丝点击