屏蔽Activity, Dialog风格Activity, AlertDialog的Home键功能方法(总结)

屏蔽Activity, Dialog风格Activity, AlertDialog的Home键功能方法分别是:


1.屏蔽Activity的Home键功能
@Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}

2.屏蔽 Dialog风格Activity的Home键功能
@Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
super.onAttachedToWindow();
}
这里之所以和Activity区分,是因为Dialog风格Activity如果用第一种方法,这个Dialog风格Activity的背景就变成黑色的,而不是透明的.

3.屏蔽AlertDialog的Home键功能
AlertDialog d = b.create();

d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);


不做屏蔽Home键的功能不能常用


在做音乐播放器的睡眠模式时,为保证在睡眠关机提示框弹出时,用户只针对当前提示框做操作,就用得屏蔽其Home键作用,就像手机要关机时,必须对关机做取消或确定关机操作一样,除非确认完关闭此对话框,不能到其它界面做操作.

                                

                                                                                                          



sleepDialog = b.create();
sleepDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
sleepDialog.show();



home键监听函数:

@Override

public boolean onKeyDown(int keyCode, KeyEvent event)

{ // TODO Auto-generated method stub

if(KeyEvent.KEYCODE_HOME==keyCode)

android.os.Process.killProcess(android.os.Process.myPid());

return super.onKeyDown(keyCode, event);

}





=============================下 面再 转载一个home键监听函数:

本文来自CSDN丹丹博客,转载请注明出处:

http://blog.csdn.net/dany1202/archive/2011/06/14/6543769.aspx

请支持原创



注册:

view plain
IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
mCloseSystemDialogsReceiver = new CloseSystemDialogsIntentReceiver();
registerReceiver(mCloseSystemDialogsReceiver, filter);

监听:

view plain
private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

String reason = intent.getStringExtra("reason");
Log.d(TAG, "CloseSystemDialogsIntentReceiver reason is " + reason);
if ("homekey".equals(reason)) {
Log.d(TAG,"homekey click=========================");
// EditWidget.this.finish();
keyHomeFlag = true;
}

}
}

上面 的函数可以在Home键功能完毕后监听到是Home键而非其它Key键.





你可能感兴趣的:(基础篇,dialog,system,filter,string,class,音乐)