Notification的功能和用法 加薪通知

时间:2025-03-17 16:07:07

实现通知栏消息的生成和消除

  1. MainActivity.java  
  2. public class MainActivity extends Activity  
  3. {  
  4.     static final int NOTIFICATION_ID = 0x123;  
  5.     NotificationManager nm;  
  6.     @Override  
  7.     public void onCreate(Bundle savedInstanceState)  
  8.     {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.main);  
  11.         // 获取系统的NotificationManager服务  
  12.         nm = (NotificationManager)  
  13.                 getSystemService(NOTIFICATION_SERVICE);  
  14.     }  
  15.     // 为发送通知的按钮的点击事件定义事件处理方法  
  16.     public void send(View source)  
  17.     {  
  18.         // 创建一个启动其他Activity的Intent  
  19.         Intent intent = new Intent(MainActivity.this  
  20.                 , OtherActivity.class);  
  21.         PendingIntent pi = PendingIntent.getActivity(  
  22.                 MainActivity.this, 0, intent, 0);  
  23.         Notification notify = new Notification.Builder(this)  
  24.                 // 设置打开该通知,该通知自动消失  
  25.                 .setAutoCancel(true)  
  26.                 // 设置显示在状态栏的通知提示信息  
  27.                 .setTicker("有新消息")  
  28.                 // 设置通知的图标  
  29.                 .setSmallIcon(R.drawable.notify)  
  30.                 // 设置通知内容的标题  
  31.                 .setContentTitle("一条新通知")  
  32.                 // 设置通知内容  
  33.                 .setContentText("恭喜你,您加薪了,工资增加20%!")  
  34.                 // 设置使用系统默认的声音、默认LED灯  
  35.                 // .setDefaults(Notification.DEFAULT_SOUND  
  36.                 // |Notification.DEFAULT_LIGHTS)  
  37.                 // 设置通知的自定义声音  
  38.                 .setSound(Uri.parse("android.resource://org.crazyit.ui/"  
  39.                         + R.raw.msg))  
  40.                 .setWhen(System.currentTimeMillis())  
  41.                 // 设改通知将要启动程序的Intent  
  42.                 .setContentIntent(pi)  // ①  
  43.                 .build();  
  44.         // 发送通知  
  45.         nm.notify(NOTIFICATION_ID, notify);  
  46.     }  
  47.     // 为删除通知的按钮的点击事件定义事件处理方法  
  48.     public void del(View v)  
  49.     {  
  50.         // 取消通知  
  51.         nm.cancel(NOTIFICATION_ID);  
  52.     }  
  53. }  
  54. OtherActivity  
  55. public class OtherActivity extends Activity  
  56. {  
  57.    @Override  
  58.    public void onCreate(Bundle savedInstanceState)  
  59.    {  
  60.       super.onCreate(savedInstanceState);  
  61.       //设置该Activity显示的页面  
  62.       setContentView(R.layout.other);  
  63.    }  
  64. }  
  65. XML文件  
  66. <?xml version="1.0" encoding="utf-8"?>  
  67. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  68.    android:orientation="horizontal"  
  69.    android:layout_width="match_parent"  
  70.    android:layout_height="match_parent"  
  71.    android:gravity="center_horizontal">  
  72. <Button  
  73.    android:layout_width="wrap_content"   
  74.    android:layout_height="wrap_content"   
  75.    android:text="发送Notification"  
  76.    android:onClick="send"  
  77.    />  
  78. <Button  
  79.    android:layout_width="wrap_content"   
  80.    android:layout_height="wrap_content"   
  81.    android:text="删除Notification"  
  82.    android:onClick="del"  
  83.    />   
  84. </LinearLayout>  
  85. other.xml  
  86. <?xml version="1.0" encoding="utf-8"?>  
  87. <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  
  88.    android:layout_width="match_parent"  
  89.    android:layout_height="wrap_content"  
  90.    android:gravity="center_horizontal"  
  91.    android:orientation="vertical">  
  92. <!-- 定义一个ImageView -->  
  93. <ImageView  
  94.    android:layout_width="match_parent"  
  95.    android:layout_height="wrap_content"  
  96.    android:src="@drawable/swift"  
  97.    android:layout_gravity="center_horizontal"  
  98.    />  
  99. </LinearLayout>  
  100. Menu.xml  
  101. <menu xmlns:android="http://schemas.android.com/apk/res/android"  
  102.      xmlns:tools="http://schemas.android.com/tools"  
  103.      tools:context=".MainActivity">  
  104.    <item android:id="@+id/action_settings"  
  105.         android:title="@string/app_name"  
  106.         android:orderInCategory="100"  
  107.         android:showAsAction="never"/>  
  108. </menu>  

效果

Notification的功能和用法 加薪通知