简体   繁体   English

按下电源按钮不会调用onPause

[英]onPause not called by hitting Powerbutton


if I hit the Powerbutton in my app while the app is currently running the onPause in not called. 如果在应用程序当前正在运行onPause时未按下该应用程序中的Powerbutton。 First question: Is this usual for Android? 第一个问题:这在Android上常见吗?
I tried to call the onPause by setting an onKeyListener . 我试图通过设置onKeyListener来调用onPause Can I set this onKeyListener for all elements in the activity? 我可以为活动中的所有元素设置此onKeyListener吗? Eg I set it on my realativeLayout . 例如,我将其设置在我的realativeLayout

rLayout.setOnKeyListener(new View.OnKeyListener()
    {
        public boolean onKey(View v, int keyCode, KeyEvent event)
        {
            if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_POWER))
            {
                onPause();
            }
            return true;
        }
    });

But this also didn't work. 但这也不起作用。 So now the main question: Do you have any possibility to call onPause if you hit the Powerbutton while the app is currently running? 因此,现在的主要问题是:如果在应用程序当前运行时按下Powerbutton,您是否有可能调用onPause (Don't need to be an onKeyListener ) (不必是onKeyListener

As your activity enters the paused state, the system calls the onPause() method 当您的活动进入暂停状态时,系统会调用onPause()方法

http://developer.android.com/training/basics/activity-lifecycle/pausing.html http://developer.android.com/training/basics/activity-lifecycle/pausing.html

It's important to note that the app itself NEVER calls onPause itself because that would likely mess up the Activity's life cycle (which is managed by Android not the app). 重要的是要注意,应用程序本身从不调用onPause本身,因为这很可能会破坏Activity的生命周期(由Android而非应用程序管理)。 If the user presses the power button, the Activity will be paused by Android automatically, there's no need to do anything in your app. 如果用户按下电源按钮,则Android会自动暂停“活动”,无需在您的应用中执行任何操作。 If you think the onPause isn't called, please post the onPause code. 如果您认为未调用onPause,请发布onPause代码。

I just want to call onPause before my phone goes into sleepmode, howsoever ... 我只是想在我的手机进入睡眠模式之前打电话给onPause ...
Here is my onPause : 这是我的onPause

@Override
protected void onPause() {
    super.onPause();
    appactive = 0;
    lastappactive = Calendar.getInstance().getTime();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM