系统运行在后台的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);
	}



好啦,就是这样啦,希望有更好的方法的朋友给点建议!





评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值