系統運行在後臺的Notification

背景:應用程序有幾個Activity, 我想在按返回鍵或者home鍵時,給出像360那種的Notification,然後點擊後返回原activity。


第一次:我在主要的Activity,也就是登錄進去後的那個Activity裏面實現了Notification,

	protected NotificationManager notificationManager = null;
	protected Notification notification = null;
	PendingIntent pendingIntent = null;


	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MContext = this;
		
		notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		notification = new Notification(R.drawable.notification,"",System.currentTimeMillis());
		notification.flags = Notification.FLAG_ONGOING_EVENT;
		pendingIntent = PendingIntent.getActivity(this, 0, getIntent(), 0);
		notification.setLatestEventInfo(this, "", "", pendingIntent);
	}
	protected void onResume() {
		// TODO Auto-generated method stub
		
		notificationManager.cancel(R.drawable.notification);
		super.onResume();
	}


	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		notificationManager.notify(R.drawable.notification,notification);
		super.onStop();
	}


如上:結果,當此Activity stop(),的確會Notification,恢復時也會消除,但是這不是我的目的。因爲這樣的話,在本應用之間的Activity跳轉都會Notification。
然後:我看到有onUserLeaveHint()這個方法,
protected void onUserLeaveHint () Since: API Level 3 Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground, onUserLeaveHint() will not be called on the activity being interrupted. In cases when it is invoked, this method is called right before the activity's onPause() callback. This callback and onUserInteraction() are intended to help activities manage status bar notifications intelligently; specifically, for helping activities determine the proper time to cancel a notfication.

但是單獨使用的時候還是不能達到目的,然後我又發現了Intent.FLAG_ACTIVITY_NO_USER_ACTION

If set, this flag will prevent the normal onUserLeaveHint() callback from occurring on the current frontmost activity before it is paused as the newly-started activity is brought to the front.

Typically, an activity can rely on that callback to indicate that an explicit user action has caused their activity to be moved out of the foreground. The callback marks an appropriate point in the activity's lifecycle for it to dismiss any notifications that it intends to display "until the user has seen them," such as a blinking LED.

If an activity is ever started via any non-user-driven events such as phone-call receipt or an alarm handler, this flag should be passed toContext.startActivity, ensuring that the pausing activity does not think the user has acknowledged its notification.

Constant Value: 262144 (0x00040000)
這樣就解決了
	
	Intent intent = new Intent(LoginActivity.this, ContacterActivity.class);
	intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
	startActivity(intent);


其餘跳轉類似,這樣的話,在各個activity跳轉就不會促發Notification了,但是按home鍵就可以觸發,還有個問題就是按back鍵時不能觸發,這好辦,我們就監聽back鍵就行了。

	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(KeyEvent.KEYCODE_BACK == keyCode){
			onUserLeaveHint();
		}
		return super.onKeyDown(keyCode, event);
	}



好啦,就是這樣啦,希望有更好的方法的朋友給點建議!





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章