需要 Android 中主页按钮的帮助
大家好,我想拦截我的主页按钮。
我想要的是,每当我按下主页按钮时,我想显示一个“您确定要退出吗”的警报对话框。如果是,则完成活动,否则不执行任何操作。 我必须知道
当我们按下主页按钮时,以下回调将按顺序执行。
onSaveInstanceState(Bundle outState)
onPause()
onStop()
所以我重写了 onSaveInstanceState 方法并在那里设置了我的警报对话框代码,但它给了我对话框异常。请各位朋友帮忙。指导我一点……关于它。
更新:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
boolean flag = displayAlertDialog();
if(flag){
this.finish();
super.onSaveInstanceState(savedInstanceState);
}
}
displayAlertDialog 方法:
private boolean isExit = false;
public boolean displayAlertDialog()
{
//final boolean flag=true;
int a = 0;
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
alt_bld.setMessage("Are you sure you want to Exit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
isExit = true;
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
isExit = false;
dialog.cancel();
}
});
alt_bld.show();
return isExit;
}
hi all i want to intercept my Home Button.
what i want is that whenever i press a Home Button i want to display a Alert dialog for Are you sure you want to Quit. if Yes then Finish the activity else do nothing.
I have got to know that
when ever we Press Home Button the following callbacks are performed in order.
onSaveInstanceState(Bundle outState)
onPause()
onStop()
So i have overRide onSaveInstanceState Method and set my Alert dialog Code there but it gives me Exception on Dialog. Please Help friends. Guide me a lil..about it.
UPDATED:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
boolean flag = displayAlertDialog();
if(flag){
this.finish();
super.onSaveInstanceState(savedInstanceState);
}
}
displayAlertDialog Method:
private boolean isExit = false;
public boolean displayAlertDialog()
{
//final boolean flag=true;
int a = 0;
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
alt_bld.setMessage("Are you sure you want to Exit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
isExit = true;
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
isExit = false;
dialog.cancel();
}
});
alt_bld.show();
return isExit;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法通过主页按钮打开对话框。但您可以通过用户选择主页或您的活动等操作来打开主页按钮上的任何活动。
you cant open dialog on home button. but you can open any activity on home button by user choose action like home or your activity..
据我所知,当按下 HOME 键时,KeyCode.KEYCODE_HOME 永远不会传播到 Activty/Dialog,HOME 键在框架中被拦截,以保证为“android.intent.category.HOME”注册的主屏幕/活动始终获得焦点。
如果应用程序允许拦截 HOME 键,则恶意应用程序可能会阻止用户退出应用程序并使用手机功能。
AFAIK KeyCode.KEYCODE_HOME is never propagated to Activty/Dialog when HOME key is pressed, HOME key is intercepted in framework to guarantee homescreen/activity registered for "android.intent.category.HOME" always brought to focus.
If intercepting HOME key is allowed in applications, there is a possibility that an evil app can prevent the user from exiting the application and using the phone functionality.
试试这个朋友....
Try this mate....