今天給大家分享下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。
下面是實現代碼:
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類:
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();
}
}
下面是通知的具體實現類:
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。
下面是效果圖:
好了這裏就講完了,希望大家有什麼好建議的可以提出來。o(∩_∩)o