Android开发之显示通知

时间:2022-05-17 10:14:14

Toast类可以用来显示消息给用户,虽然它很方便,但是有不能持久。它只是在屏幕上显示几秒后就自动消失掉了。对于重要的信息或消息,要使用更加持久的方法。这种情形下,就应当使用通知,即使用NotificationManager在设备顶部的状态栏(也叫做通知栏)中显示一条持久化的信息或消息。

要显示一个通知,首先要创建一个指向NotificationView类 Intent对象:

Intent intent = new Intent(this,NotificationViewActivity.class);
intent.putExtra("notificationID", notificationID);

当用户从通知列表中选择一个通知的时候,这个Intent就被用来启动另一个活动。

另外还需要创建一个PendingIntent对象。PendingIntent对象可以代表应用程序帮助您在后面某个时候执行一个动作,而不用考虑应用程序是否正在运行。一般如下初始化:

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

getActivity()方法检索一个PendingIntent对象并使如下参数设置它:

上下文-应用程序上下文

请求码-用于意图的请求码

意图-用来启动目标活动的意图

标志-活动启动时使用的标志

然后,获取一个NotificationManager类的实例并创建一个Notification类的实例:

NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(
R.drawable.ic_launcher,
"Reminder:Meeting stats in 5 minutes",
System.currentTimeMillis());

接下来,使用setLatestEventInfo()方法来设置通知的详细内容:

CharSequence from = "System Alarm";
CharSequence message = "Meeting with cusomer at 3pm..."; notification.setLatestEventInfo(this, from, message, pendingIntent); notification.vibrate = new long[]{100,250,100,500};//设置通知为震动手机形式
nm.notify(notificationID, notification);

当用户点击通知时候,NotificationViewActivity活动就会启动,这里使用NotificationManager对象的cancel()方法来取消这个通知:

NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.cancel(getIntent().getExtras().getInt("notificationID"));

完整实例代码如下:

MainActivity活动类:

package com.example.notifications;

import android.os.Bundle;
import android.R.integer;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.Menu;
import android.view.View; public class MainActivity extends Activity { private int notificationID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view){
displayNotification();
}
protected void displayNotification(){
Intent intent = new Intent(this,NotificationViewActivity.class);
intent.putExtra("notificationID", notificationID); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(
R.drawable.ic_launcher,
"Reminder:Meeting stats in 5 minutes",
System.currentTimeMillis());
CharSequence from = "System Alarm";
CharSequence message = "Meeting with cusomer at 3pm..."; notification.setLatestEventInfo(this, from, message, pendingIntent); notification.vibrate = new long[]{100,250,100,500};
nm.notify(notificationID, notification); }
// @Override
// public boolean onCreateOptionsMenu(Menu menu) {
// // Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.main, menu);
// return true;
// } }

NotificationViewActivity活动类:

package com.example.notifications;

import android.os.Bundle;
import android.app.Activity;
import android.app.NotificationManager;
import android.view.Menu; public class NotificationViewActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_view); NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.cancel(getIntent().getExtras().getInt("notificationID"));
} }