如何显示警告对话框时,硬件主页按钮pressed?

编程入门 行业动态 更新时间:2024-10-23 07:16:55
本文介绍了如何显示警告对话框时,硬件主页按钮pressed?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我用下面的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?

本文发布于:2023-11-27 21:06:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639603.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对话框   按钮   硬件   主页   pressed

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!