Android应用层怎样屏蔽home键,网上找到的方法都不能实现,有谁有测试过能行的
方法吗。
重寫onbackpress()方法,返回null
在2.3版本以下重写下面方法就能重写home键
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
在4.0以上的版本中需要利用以下方法屏蔽和重写Home键,代码如下:
public static final int FLAG_HOMEKEY_DISPATCHED = 0x80000000; //需要自己定义标志
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setFlags(FLAG_HOMEKEY_DISPATCHED, FLAG_HOMEKEY_DISPATCHED);//关键代码
setContentView(R.layout.main);
}
再重写onKey事件即可。
@Override
public boolean onKeyDown( int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == event. KEYCODE_HOME) {
return true;
}
return super.onKeyDown(keyCode, event);
}
PS:
在AndroidMainfest.xml需要加权限:
<!-- 屏蔽HOME键需要的权限 -->
在配置文件中,在你使用了Notification的activity中加一个属性android: android:launchMode="singleInstance"
你百度一下,好像有一个在activity里面有一个方法,只要屏蔽就ok了
class MyTimerTask extends TimerTask {
@Override
public void run() {
bringApplicationToFront();
}
}
private void bringApplicationToFront() {
if (!onHomedown)
return;
KeyguardManager myKeyManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
if (myKeyManager.inKeyguardRestrictedInputMode())
return;
Log.d("Test", "====Bringging Application to Front====");
Intent notificationIntent = new Intent(this, AppActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
try {
pendingIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
onHomedown = false;
}
@Override
protected void onResume() {
super.onResume();
if (timer != null) {
timer.cancel();
timer = null;
}
Log.d("AppActivity", "resume");
}