亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

【Android 開發(fā)教程】Notification通知

系統(tǒng) 1932 0

本章節(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中的文件。

  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. < TextView
  7. android:layout_width = "fill_parent"
  8. android:layout_height = "wrap_content"
  9. android:text = "Herearethedetailsforthenotification..." />
  10. </ LinearLayout >

4.NotificationView.java中的代碼。
  1. public class NotificationView extends Activity{
  2. @Override
  3. public void onCreate(BundlesavedInstanceState){
  4. super .onCreate(savedInstanceState);
  5. setContentView(R.layout.notification);
  6. //---lookupthenotificationmanagerservice---
  7. NotificationManagernm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  8. //---cancelthenotificationthatwestarted---
  9. nm.cancel(getIntent().getExtras().getInt( "notificationID" ));
  10. }
  11. }

5. AndroidManifest.xml中的代碼。

  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. < uses-sdk android:minSdkVersion = "14" />
  7. < uses-permission android:name = "android.permission.VIBRATE" />
  8. < application
  9. android:icon = "@drawable/ic_launcher"
  10. android:label = "@string/app_name" >
  11. < activity
  12. android:label = "@string/app_name"
  13. android:name = ".NotificationsActivity" >
  14. < intent-filter >
  15. < action android:name = "android.intent.action.MAIN" />
  16. < category android:name = "android.intent.category.LAUNCHER" />
  17. </ intent-filter >
  18. </ activity >
  19. < activity android:name = ".NotificationView"
  20. android:label = "Detailsofnotification" >
  21. < intent-filter >
  22. < action android:name = "android.intent.action.MAIN" />
  23. < category android:name = "android.intent.category.DEFAULT" />
  24. </ intent-filter >
  25. </ activity >
  26. </ application >
  27. </ manifest >
6. main.xml中的代碼。
  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. < Button
  7. android:id = "@+id/btn_displaynotif"
  8. android:layout_width = "fill_parent"
  9. android:layout_height = "wrap_content"
  10. android:text = "DisplayNotification"
  11. android:onClick = "onClick" />
  12. </ LinearLayout >
7. 最后,NotificationActivity.java中的代碼。
  1. public class NotificationsActivity extends Activity{
  2. int notificationID= 1 ;
  3. /**Calledwhentheactivityisfirstcreated.*/
  4. @Override
  5. public void onCreate(BundlesavedInstanceState){
  6. super .onCreate(savedInstanceState);
  7. setContentView(R.layout.main);
  8. }
  9. public void onClick(Viewview){
  10. displayNotification();
  11. }
  12. protected void displayNotification()
  13. {
  14. //---PendingIntenttolaunchactivityiftheuserselects
  15. //thisnotification---
  16. Intenti= new Intent( this ,NotificationView. class );
  17. i.putExtra( "notificationID" ,notificationID);
  18. PendingIntentpendingIntent=
  19. PendingIntent.getActivity( this , 0 ,i, 0 );
  20. NotificationManagernm=(NotificationManager)
  21. getSystemService(NOTIFICATION_SERVICE);
  22. Notificationnotif= new Notification(
  23. R.drawable.ic_launcher,
  24. "Reminder:Meetingstartsin5minutes" ,
  25. System.currentTimeMillis());
  26. CharSequencefrom= "SystemAlarm" ;
  27. CharSequencemessage= "Meetingwithcustomerat3pm..." ;
  28. notif.setLatestEventInfo( this ,from,message,pendingIntent);
  29. //---100msdelay,vibratefor250ms,pausefor100msand
  30. //thenvibratefor500ms---
  31. notif.vibrate= new long []{ 100 , 250 , 100 , 500 };
  32. nm.notify(notificationID,notif);
  33. }
  34. }
8. 調試。

9. 點擊Display Notification按鈕,在狀態(tài)欄上面就會出現(xiàn)一個notification通知。如圖:

【Android 開發(fā)教程】Notification通知

10.將狀態(tài)欄拉下來,就會顯示這個Notification通知的詳盡信息。如圖:

【Android 開發(fā)教程】Notification通知

11. 點擊這個Notification通知,就會顯示NotificationView的界面,同時,狀態(tài)欄上面的通知也消失了。如圖:

【Android 開發(fā)教程】Notification通知

【Android 開發(fā)教程】Notification通知


更多文章、技術交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 九九国产精品九九 | 久久成人永久免费播放 | 久热精品视频在线观看 | 四虎精品影院永久在线播放 | 久久国产精品免费观看 | 日日噜噜夜夜狠狠久久aⅴ 日日噜噜夜夜狠狠久久丁香 | 久久精品99精品免费观看 | 四虎影院网站 | 99久久免费中文字幕精品 | 高清黄色毛片 | 五月天亚洲视频 | 国产精品18久久久久久小说 | 青青青免费手机版视频在线观看 | 天天色综合色 | 国产精品综合一区二区 | 国产我不卡| 日本不卡视频 | 久久视频免费在线观看 | 日本精品视频一视频高清 | 毛片破处| 亚洲国产综合人成综合网站00 | 国产在线原创剧情麻豆 | 一本岛高清v不卡免费一三区 | 老色鬼a∨在线视频在线观看 | 久久999 | 久久精品国产一区二区三区肥胖 | 狠狠色噜噜狠狠狠狠色吗综合 | 中国日韩欧美中文日韩欧美色 | 成人在线视频免费观看 | 久久久久久97 | 久久综合偷偷噜噜噜色 | 日本边添边爱边做视频 | 欧美大交乱xxxxbbbb | 99色在线视频 | 毛片小视频| 西西亚洲 | 国产区在线视频 | 久久精品国产视频 | 国产精品欧美久久久久天天影视 | 国产影片中文字幕 | 美女色片|