我用下面的code,显示一个警告对话框,当硬件home键是pressed。
I had used the following code to show an alert dialog when Hardware home button is pressed.
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_HOME)) { System.out.println("KEYCODE_HOME"); showDialog("'HOME'"); return true; } if ((keyCode == KeyEvent.KEYCODE_BACK)) { System.out.println("KEYCODE_BACK"); showDialog("'BACK'"); return true; } if ((keyCode == KeyEvent.KEYCODE_MENU)) { System.out.println("KEYCODE_MENU"); showDialog("'MENU'"); return true; } return false; } void showDialog(String the_key){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("You have pressed the " + the_key + " button. Would you like to exit the app?") .setCancelable(true) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); finish(); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert = builder.create(); alert.setTitle("CoderzHeaven."); alert.show(); } @Override public void onAttachedToWindow() { super.onAttachedToWindow(); this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD); } public void onUserLeaveHint() { super.onUserLeaveHint(); System.out.println("HOMEEEEEEEEE"); }这工作在API版本低于14,细而在较高版本的应用程序被撞坏表示无差错之后增加一个窗口,窗口不能改变。而我才知道错误是由于该行
This works fine in api version less than 14. But in higher versions app gets crashed showing an error- "Window cannot be changed after a window is added". And i came to know the error is due to this line
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);当我把这个行注释的警告对话框中没有显示。反正是有显示警告对话框更高版本?
When i put this line in comment the alert dialog is not showing. Is there anyway to show alert dialog in higher versions?
推荐答案从技术上讲,你水湿覆盖home键。如果你仍然有兴趣,你可以尝试把这个在您的主要活动在AndroidManifest.xml
Technically you cann't override home button. If you still interested, you could try putting this in your main activity in AndroidManifest.xml
<activity ... android:launchMode="singleTask"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.HOME" /> <category android:name="android.intent.category.DEFAULT" /> .... </intent-filter>
更多推荐
如何显示警告对话框时,硬件主页按钮pressed?
发布评论