WenJieWangFlyToWorld

导航

Notification的功能和用法 加薪通知

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

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

 

效果

 

posted on 2017-06-01 20:04  WenjieWangFlyToWorld  阅读(369)  评论(0编辑  收藏  举报