本章节翻译自《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中的文件。
- <?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="Here are the details for the notification..." />
- </LinearLayout>
4.NotificationView.java中的代码。
- public class NotificationView extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.notification);
- // ---look up the notification manager service---
- NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- // ---cancel the notification that we started---
- 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="Details of notification">
- <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="Display Notification"
- android:onClick="onClick"/>
- </LinearLayout>
- public class NotificationsActivity extends Activity {
- int notificationID = 1;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- public void onClick(View view) {
- displayNotification();
- }
- protected void displayNotification()
- {
- //---PendingIntent to launch activity if the user selects
- // this notification---
- Intent i = new Intent(this, NotificationView.class);
- i.putExtra("notificationID", notificationID);
- PendingIntent pendingIntent =
- PendingIntent.getActivity(this, 0, i, 0);
- NotificationManager nm = (NotificationManager)
- getSystemService(NOTIFICATION_SERVICE);
- Notification notif = new Notification(
- R.drawable.ic_launcher,
- "Reminder: Meeting starts in 5 minutes",
- System.currentTimeMillis());
- CharSequence from = "System Alarm";
- CharSequence message = "Meeting with customer at 3pm...";
- notif.setLatestEventInfo(this, from, message, pendingIntent);
- //---100ms delay, vibrate for 250ms, pause for 100 ms and
- // then vibrate for 500ms---
- notif.vibrate = new long[] { 100, 250, 100, 500};
- nm.notify(notificationID, notif);
- }
- }
9. 点击Display Notification按钮,在状态栏上面就会出现一个notification通知。如图:
10.将状态栏拉下来,就会显示这个Notification通知的详尽信息。如图:
11. 点击这个Notification通知,就会显示NotificationView的界面,同时,状态栏上面的通知也消失了。如图: