1. 避免App点击Home键后点击应用桌面图标重新启动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
settings= Settings.getInstance(this);
/*********避免App点击Home键后点击应用桌面图标重新启动*********/
if(!this.isTaskRoot()){
Intent intent = getIntent();
if(intent !=null){
String action = intent.getAction();
if(intent.hasCategory(Intent.CATEGORY_LAUNCHER)&&(Intent.ACTION_MAIN.equals(action))){
finish();
return;
}
}
}
}
2. Home和Back键的监听事件
(1) Home键的监听事件
@Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
}
(2) Back键的监听事件
@Override
public void onBackPressed() {
super.onBackPressed();
}
3. 禁用Android系统Home键或Back键
关键在于监听并复写事件(需了解事件分发机制)
(1) 禁用Back键
@Override
public void onBackPressed() {
//super.onBackPressed();
}
或
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
return true;
}
}
return super.onKeyDown(keyCode, event); // return false;
}
(2) 禁用Home键
注意:Android4.0后,在应用层无法屏蔽home键,也监听不到HOME键(Home键的KeyEvent不会送到application层来,在framework就已经处理完了,所以没办法屏蔽)
网友评论