到目前为止,想必
大家已经都熟悉使用Toast去给用户显示信息了。尽管使用Toast很方便,但是Toast显示的通知并不是永久存储的。它只在屏幕上显示一小段时间,然后就消失了。如果它包含一些特别重要的信息,如果用户没有观察屏幕,那么用户就很容易错过它。
对于那些重要的信息,应该采用一种更加持久保存的方法。在这种情况下,应该使用NotificationMnanger(消息管理器)去显示一个长久的信息,这个消息被显示在了StatusBar(状态栏)上面,使用用户能够很容易地看见。
接下来展示如何发送一个Notification通知。
1.创建一个名为Notifications的工程。
2.在包中新建一个名为NotificationView的类,同时在res/layout文件夹下面新建一个名为notification.xml 文件,它将作为NotificationView的视图。
3.notification.xml中的文件。
[html] view plaincopy
<?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中的代码。
package net.learn2develop.Notifications;
import
Android.app.Activity;
import
Android.app.NotificationManager;
import
Android.os.Bundle;
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中的代码。
[html] view plaincopy
<?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>
6.main.xml中的代码。
[html] view plaincopy
<?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>
7.最后,NotificationActivity.
java中的代码。
package net.learn2develop.Notifications;
import
Android.app.Activity;
import
Android.app.Notification;
import
Android.app.NotificationManager;
import
Android.app.PendingIntent;
import
Android.content.Intent;
import
Android.os.Bundle;
import
Android.view.View;
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());
www.atcpu.com 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);
}
}
8.调试。9.点击Display Notification按钮,在状态栏上面就会出现一个notification通知。如图:
10.将状态栏拉下来,就会显示这个Notification通知的详尽信息。如图:
11.点击这个Notification通知,就会显示NotificationView的界面,同时,状态栏上面的通知也消失了。如图:
摘自 manoel的专栏