本章節(jié)翻譯自《Beginning-Android-4-Application-Development》,如有翻譯不當?shù)牡胤剑凑堉赋觥?
原書購買地址 http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/
到目前為止,想必大家已經(jīng)都熟悉使用Toast去給用戶顯示信息了。盡管使用Toast很方便,但是Toast顯示的通知并不是永久存儲的。它只在屏幕上顯示一小段時間,然后就消失了。如果它包含一些特別重要的信息,如果用戶沒有觀察屏幕,那么用戶就很容易錯過它。
對于那些重要的信息,應該采用一種更加持久保存的方法。在這種情況下,應該使用NotificationMnanger(消息管理器)去顯示一個長久的信息,這個消息被顯示在了StatusBar(狀態(tài)欄)上面,使用用戶能夠很容易地看見。
接下來展示如何發(fā)送一個Notification通知。
1. 創(chuàng)建一個工程:Notifications。
2. 在包中新建一個名為NotificationView的類,同時在res/layout文件夾下面新建一個名為notification.xml 文件,它將作為NotificationView的視圖。
3. notification.xml中的文件。
- <? xml version = "1.0" encoding = "utf-8" ?>
- < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
- android:layout_width = "fill_parent"
- android:layout_height = "fill_parent"
- android:orientation = "vertical" >
- < TextView
- android:layout_width = "fill_parent"
- android:layout_height = "wrap_content"
- android:text = "Herearethedetailsforthenotification..." />
- </ LinearLayout >
4.NotificationView.java中的代碼。
- public class NotificationView extends Activity{
- @Override
- public void onCreate(BundlesavedInstanceState){
- super .onCreate(savedInstanceState);
- setContentView(R.layout.notification);
- //---lookupthenotificationmanagerservice---
- NotificationManagernm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
- //---cancelthenotificationthatwestarted---
- nm.cancel(getIntent().getExtras().getInt( "notificationID" ));
- }
- }
5. AndroidManifest.xml中的代碼。
- <? xml version = "1.0" encoding = "utf-8" ?>
- < manifest xmlns:android = "http://schemas.android.com/apk/res/android"
- package = "net.learn2develop.Notifications"
- android:versionCode = "1"
- android:versionName = "1.0" >
- < uses-sdk android:minSdkVersion = "14" />
- < uses-permission android:name = "android.permission.VIBRATE" />
- < application
- android:icon = "@drawable/ic_launcher"
- android:label = "@string/app_name" >
- < activity
- android:label = "@string/app_name"
- android:name = ".NotificationsActivity" >
- < intent-filter >
- < action android:name = "android.intent.action.MAIN" />
- < category android:name = "android.intent.category.LAUNCHER" />
- </ intent-filter >
- </ activity >
- < activity android:name = ".NotificationView"
- android:label = "Detailsofnotification" >
- < intent-filter >
- < action android:name = "android.intent.action.MAIN" />
- < category android:name = "android.intent.category.DEFAULT" />
- </ intent-filter >
- </ activity >
- </ application >
- </ manifest >
- <? xml version = "1.0" encoding = "utf-8" ?>
- < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
- android:layout_width = "fill_parent"
- android:layout_height = "fill_parent"
- android:orientation = "vertical" >
- < Button
- android:id = "@+id/btn_displaynotif"
- android:layout_width = "fill_parent"
- android:layout_height = "wrap_content"
- android:text = "DisplayNotification"
- android:onClick = "onClick" />
- </ LinearLayout >
- public class NotificationsActivity extends Activity{
- int notificationID= 1 ;
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- public void onCreate(BundlesavedInstanceState){
- super .onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- public void onClick(Viewview){
- displayNotification();
- }
- protected void displayNotification()
- {
- //---PendingIntenttolaunchactivityiftheuserselects
- //thisnotification---
- Intenti= new Intent( this ,NotificationView. class );
- i.putExtra( "notificationID" ,notificationID);
- PendingIntentpendingIntent=
- PendingIntent.getActivity( this , 0 ,i, 0 );
- NotificationManagernm=(NotificationManager)
- getSystemService(NOTIFICATION_SERVICE);
- Notificationnotif= new Notification(
- R.drawable.ic_launcher,
- "Reminder:Meetingstartsin5minutes" ,
- System.currentTimeMillis());
- CharSequencefrom= "SystemAlarm" ;
- CharSequencemessage= "Meetingwithcustomerat3pm..." ;
- notif.setLatestEventInfo( this ,from,message,pendingIntent);
- //---100msdelay,vibratefor250ms,pausefor100msand
- //thenvibratefor500ms---
- notif.vibrate= new long []{ 100 , 250 , 100 , 500 };
- nm.notify(notificationID,notif);
- }
- }
9. 點擊Display Notification按鈕,在狀態(tài)欄上面就會出現(xiàn)一個notification通知。如圖:
10.將狀態(tài)欄拉下來,就會顯示這個Notification通知的詳盡信息。如圖:
11. 點擊這個Notification通知,就會顯示NotificationView的界面,同時,狀態(tài)欄上面的通知也消失了。如圖:

更多文章、技術交流、商務合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
