我正在尝试在服务运行时阻止HOME按钮KeyPress
protected void onPause () {
if (isMyServiceRunning())
{
Intent Act2Intent = new Intent(PhysicalTheftDialog.this, PhysicalTheftDialog.class);
startActivity(Act2Intent);
}
else {
}
}
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("nyp.android.project.MyService".equals(service.service.getClassName())) {
return true;
}
}
return false;
}
但每当我按下HOME按钮时,应用程序都会崩溃。
答案 0 :(得分:1)
请参阅,出于安全原因,Android开发人员本身不允许我们使用主页按钮更改任何类型的行为。但即使您确实要禁用主页按钮,也可以通过添加以下代码来执行此操作:
@Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME) {
Log.i("Home Button","Clicked");
}
if(keyCode==KeyEvent.KEYCODE_BACK) {
finish();
}
return false;
}