本章节翻译自《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. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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中的代码。
[java] view plain copy
  1. publicclassNotificationViewextendsActivity{
  2. @Override
  3. publicvoidonCreate(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中的代码。

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  3. package="net.learn2develop.Notifications"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <uses-sdkandroid:minSdkVersion="14"/>
  7. <uses-permissionandroid: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. <actionandroid:name="android.intent.action.MAIN"/>
  16. <categoryandroid:name="android.intent.category.LAUNCHER"/>
  17. </intent-filter>
  18. </activity>
  19. <activityandroid:name=".NotificationView"
  20. android:label="Detailsofnotification">
  21. <intent-filter>
  22. <actionandroid:name="android.intent.action.MAIN"/>
  23. <categoryandroid:name="android.intent.category.DEFAULT"/>
  24. </intent-filter>
  25. </activity>
  26. </application>
  27. </manifest>
6. main.xml中的代码。 [html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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中的代码。 [java] view plain copy
  1. publicclassNotificationsActivityextendsActivity{
  2. intnotificationID=1;
  3. /**Calledwhentheactivityisfirstcreated.*/
  4. @Override
  5. publicvoidonCreate(BundlesavedInstanceState){
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.main);
  8. }
  9. publicvoidonClick(Viewview){
  10. displayNotification();
  11. }
  12. protectedvoiddisplayNotification()
  13. {
  14. //---PendingIntenttolaunchactivityiftheuserselects
  15. //thisnotification---
  16. Intenti=newIntent(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=newNotification(
  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=newlong[]{100,250,100,500};
  32. nm.notify(notificationID,notif);
  33. }
  34. }
8. 调试。

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

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

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


更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. nfs: server 192.168.0.3 not responding, still trying
  3. Android调用camera错误setParameters failed深层解析
  4. android典型代码系列(二十一)------根据文件后缀名获得对应的MIM
  5. Android实践 -- 监听应用程序的安装、卸载
  6. Android处理图像数据转换的各种方法
  7. Android(安卓)源代码下载
  8. Android读取SIM联系人信息
  9. Android(安卓)App启动图启动界面(Splash)的简单实现

随机推荐

  1. java实现电脑开关机
  2. Android(安卓)水波纹效果的探究
  3. 太火了!MyBatis Plus 为啥这么牛?
  4. HBase 底层原理详解(深度好文,建议收藏)
  5. 循环结构的继续学习
  6. 工作中常用到的Linux命令
  7. 太简单了!PHP获取文件扩展名的7中方法
  8. php中使用fsockopen实现异步请求(代码示例
  9. php实现将表单内容提交到数据库
  10. 流行的php rpc框架详解