需要 Android 中主页按钮的帮助

发布于 2024-11-08 12:05:48 字数 1531 浏览 6 评论 0原文

大家好,我想拦截我的主页按钮。
我想要的是,每当我按下主页按钮时,我想显示一个“您确定要退出吗”的警报对话框。如果是,则完成活动,否则不执行任何操作。 我必须知道
当我们按下主页按钮时,以下回调将按顺序执行。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

逆蝶 2024-11-15 12:05:48

您无法通过主页按钮打开对话框。但您可以通过用户选择主页或您的活动等操作来打开主页按钮上的任何活动。

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..

以酷 2024-11-15 12:05:48

据我所知,当按下 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.

流星番茄 2024-11-15 12:05:48

试试这个朋友....

 @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
        if ((keyCode == KeyEvent.KEYCODE_HOME)) {    
         Log.d(this.getClass().getName(), "home button pressed");   
      }  
       return super.onKeyDown(keyCode, event);
     } 

Try this mate....

 @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
        if ((keyCode == KeyEvent.KEYCODE_HOME)) {    
         Log.d(this.getClass().getName(), "home button pressed");   
      }  
       return super.onKeyDown(keyCode, event);
     } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文