【Android 开发教程】Notification通知

本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。

原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/


到目前为止,想必大家已经都熟悉使用Toast去给用户显示信息了。尽管使用Toast很方便,但是Toast显示的通知并不是永久存储的。它只在屏幕上显示一小段时间,然后就消失了。如果它包含一些特别重要的信息,如果用户没有观察屏幕,那么用户就很容易错过它。

对于那些重要的信息,应该采用一种更加持久保存的方法。在这种情况下,应该使用NotificationMnanger(消息管理器)去显示一个长久的信息,这个消息被显示在了StatusBar(状态栏)上面,使用用户能够很容易地看见。

接下来展示如何发送一个Notification通知。

1. 创建一个工程:Notifications。

2. 在包中新建一个名为NotificationView的类,同时在res/layout文件夹下面新建一个名为notification.xml 文件,它将作为NotificationView的视图。

3. notification.xml中的文件。

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="Here are the details for the notification..." />  
  11.   
  12. </LinearLayout>  

4.NotificationView.java中的代码。
[java] view plain copy
  1. public class NotificationView extends Activity {  
  2.     @Override  
  3.     public void onCreate(Bundle savedInstanceState) {  
  4.         super.onCreate(savedInstanceState);  
  5.         setContentView(R.layout.notification);  
  6.   
  7.         // ---look up the notification manager service---  
  8.         NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  9.   
  10.         // ---cancel the notification that we started---  
  11.         nm.cancel(getIntent().getExtras().getInt("notificationID"));  
  12.     }  
  13. }  

5. AndroidManifest.xml中的代码。

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="net.learn2develop.Notifications"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="14" />  
  8.     <uses-permission android:name="android.permission.VIBRATE"/>  
  9.   
  10.     <application  
  11.         android:icon="@drawable/ic_launcher"  
  12.         android:label="@string/app_name" >  
  13.         <activity  
  14.             android:label="@string/app_name"  
  15.             android:name=".NotificationsActivity" >  
  16.             <intent-filter >  
  17.                 <action android:name="android.intent.action.MAIN" />  
  18.   
  19.                 <category android:name="android.intent.category.LAUNCHER" />  
  20.             </intent-filter>  
  21.         </activity>          
  22.         <activity android:name=".NotificationView"   
  23.             android:label="Details of notification">  
  24.             <intent-filter>  
  25.                 <action android:name="android.intent.action.MAIN" />   
  26.                 <category android:name="android.intent.category.DEFAULT" />   
  27.             </intent-filter>  
  28.         </activity>              
  29.     </application>  
  30.   
  31. </manifest>  
6. main.xml中的代码。
[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7. <Button  
  8.     android:id="@+id/btn_displaynotif"  
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"  
  11.     android:text="Display Notification"   
  12.     android:onClick="onClick"/>  
  13.   
  14. </LinearLayout>  
7. 最后,NotificationActivity.java中的代码。
[java] view plain copy
  1. public class NotificationsActivity extends Activity {  
  2.     int notificationID = 1;  
  3.       
  4.     /** Called when the activity is first created. */  
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.main);  
  9.     }  
  10.       
  11.     public void onClick(View view) {  
  12.         displayNotification();  
  13.     }  
  14.       
  15.     protected void displayNotification()  
  16.     {  
  17.         //---PendingIntent to launch activity if the user selects  
  18.         // this notification---  
  19.         Intent i = new Intent(this, NotificationView.class);  
  20.         i.putExtra("notificationID", notificationID);  
  21.   
  22.         PendingIntent pendingIntent =  
  23.             PendingIntent.getActivity(this0, i, 0);  
  24.   
  25.         NotificationManager nm = (NotificationManager)  
  26.             getSystemService(NOTIFICATION_SERVICE);   
  27.   
  28.         Notification notif = new Notification(  
  29.             R.drawable.ic_launcher,   
  30.             "Reminder: Meeting starts in 5 minutes",  
  31.             System.currentTimeMillis());  
  32.   
  33.         CharSequence from = "System Alarm";  
  34.         CharSequence message = "Meeting with customer at 3pm...";  
  35.           
  36.         notif.setLatestEventInfo(this, from, message, pendingIntent);  
  37.   
  38.         //---100ms delay, vibrate for 250ms, pause for 100 ms and  
  39.         // then vibrate for 500ms---  
  40.         notif.vibrate = new long[] { 100250100500};  
  41.         nm.notify(notificationID, notif);          
  42.     }  
  43.   
  44. }  
8. 调试。

9. 点击Display Notification按钮,在状态栏上面就会出现一个notification通知。如图:

10.将状态栏拉下来,就会显示这个Notification通知的详尽信息。如图:

11. 点击这个Notification通知,就会显示NotificationView的界面,同时,状态栏上面的通知也消失了。如图:


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值