今天給大家分享下Notification的使用,下面這個例子是通過將應用放到後台運行並在通知爛顯示應用正在運行,點擊通知返回最後操作的activity。
首先介紹下我的環境,我用的android1.6,大家都知道把程式放入後台除代碼實現外,還可以按Home鍵,但是只有在2.0以後才能監聽Home鍵事件,通過從寫onAttachedToWindow ()方法然後把改變視窗的類型為WindowManager.LayoutParams.TYPE_KEYGUARD這樣就能在onEvent()方法中攔截。但是1.6就比較蛋疼了,我採用的方法比較笨,雖然不能攔截Home鍵,但是我們可以利用Activity的生命週期來實現,當一個Activity從螢幕上消失時,調用的方法是onStop()---onPause()方法,其實這2個方法我們都可以使用。就是在這個方法裡我們要判斷我們當前的操作是Activity和Activity之間的跳轉過程還是因為按了Home鍵才觸發的,首先我定義了一個boolean屬性來標識這個值,當我每次跳轉activity的是時候就把這個值設定為true,反之則設定為false並且取消通知顯示。如果當這個值為false的時候就把程式放到後台。這裡需要注意的是如果是activity方法跳轉這裡需要在onResume()方法中再把這個boolean值設定成false。
下面是實現代碼:
[java]
import android.app.Activity;
import android.os.Bundle;
import com.metarnet.customize.view.NotificationExtend;
public class BaseActivity extends Activity {
public NotificationExtend notify;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onResume() {
super.onResume();
if (notify != null) {
notify.cancelNotification();
}
}
protected void moveTaskToBack() {
notify = new NotificationExtend(this);
notify.showNotification();
}
}
這是我寫的一個activity的一個基類,方便每個activity實現功能。
下面是具體實現的activity類:
[java]
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.KeyEvent;
import com.metarnet.R;
/**
*
* @author yangzhiqiang
*
*/
public class LoginActivity extends BaseActivity {
private boolean isForward = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// 如果是返回鍵就提示使用者是否要將程式放入後台
returnMaskTask();
}
return true;
}
private void returnMaskTask() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.common_prompt);
builder.setMessage(R.string.login_alert_exit_info);
builder.setNeutralButton(R.string.common_backgrounp,
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//點擊放入後台按鈕就將調用moveTaskToBack(true)方法將程式放入後台這時候會調用onPause()方法
//這是在onPause()方法中會調用父類的啟動通知的方法
isForward = false;
moveTaskToBack(true);
}
});
builder.setNegativeButton(R.string.common_cancel, null);
builder.show();
}
@Override
protected void onResume() {
isForward = false;
super.onResume();
}
@Override
protected void onPause() {
if (!isForward) {
super.moveTaskToBack();
}
super.onPause();
}
}
下面是通知的具體實作類別:
[java]
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import com.metarnet.R;
/**
* 顯示通知
*
* @author yangzhiqiang
*
*/
public class NotificationExtend {
private Context context;
public NotificationExtend(Context context) {
super();
this.context = context;
}
public void showNotification() {
NotificationManager manager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon,
context.getString(R.string.app_name),
System.currentTimeMillis());
// 設定通知不能被清除按鈕清除也可以設定為FLAG_AUTO_CANCEL,表示可以清楚
notification.flags |= Notification.FLAG_NO_CLEAR;
// 這個標識將通知放入通知的正在運行欄目
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults = Notification.DEFAULT_LIGHTS;
// 這裡我是自訂通知的布局
notification.contentView = new RemoteViews(context.getPackageName(),
R.layout.custoim_notifition);
notification.contentView.setImageViewResource(R.id.image,
R.drawable.icon);
notification.contentView.setTextViewText(R.id.title,
context.getString(R.string.app_name));
notification.contentView.setTextViewText(R.id.text,
context.getString(R.string.notify_content));
try {
// 這裡設定點擊通知後將放回的activity,我這裡設定的返回原activity
Intent intent = new Intent(context, context.getClass());
// 這裡是設定activity的啟動模式,分別有4種,我這裡是用的FLAG_ACTIVITY_SINGLE_TOP作用是在返回的時候用原來的activity的執行個體而不是建立新的
// 應為這個時候當前的activity在當前的activity棧頂
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pi = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.contentIntent = pi;
manager.notify(0, notification);
} catch (Exception e) {
e.printStackTrace();
}
}
public void cancelNotification() {
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
notificationManager.cancel(0);
}
}
其中PendingIntent中的PendingIntent.FLAG_UPDATE_CURRENT屬性的作用是如果我在從系統中提取一個PendingIntent,而系統中有一個和你描述的PendingIntent對等的PendingInent, 那麼系統會直接返回和該PendingIntent其實是同一token的PendingIntent,而不是一個新的token的PendingIntent。如果我們使用了FLAG_UPDATE_CURRENT的話,新的Intent會更新之前PendingIntent中的Intent對象資料,當然也會更新Intent中的Extras。
下面是: