开发者

Activity lifecycle during phone display timeouts

开发者 https://www.devze.com 2023-02-07 05:59 出处:网络
Today I noticed something rather interesting.The activity lifecycle seems to have a slight discrepancy when the phone display times out.Let me explain with an example.

Today I noticed something rather interesting. The activity lifecycle seems to have a slight discrepancy when the phone display times out. Let me explain with an example.

I have an activity that uses the accelerometer to vibrate the phone. In the onPause event I unregister the SensorManager listener so that I don't vibrate the phone when my activity is no longer the main focus.

However, I've noticed if the display shuts off and then comes back on my activity has the SensorManager listener registered even before I unlock the screen, enter my password, and my activity is shown.

Now I realize this is my own interpretation of how I would expect it to work, but to me this seems rather strange, since my activity isn't yet the main focus. I expected the SensorManager listener wasn't registered because onResume hasn't yet been called. This clearl开发者_StackOverflow社区y isn't the case when I can make my phone vibrate from both the lock screen and the password screen.

So, can anyone explain why this behavior? Secondly what can I do to get around it?

Thank you.

EDIT for clarity

I use the accelerometer to trigger a vibrate by moving the phone. This is accomplished through the SensorManager listener.

Scenario:

My activity is in the foreground (main focus). I trigger the vibrate by moving the phone. The display times out. At this point I can not trigger the vibrate. I press home/power to show the screen. I can now vibrate my phone even though either the lock screen or password screen is shown and my activity is not in the foreground.

I can not verify if the reason I can not vibrate the phone when the display turns off is because onPause was called or there is something inherent to the OS that prevents it. Or to put it another way, I also can't verify if onResume was called when the display turned on.

The key to all this is understanding the activity life cycle when the phones display is shut off. Unfortunately, my expectation was that it would follow the same life cycle diagram that we have all learned. My opinion was that it is different.


You said it.

"expected the SensorManager listener wasn't registered because onResume hasn't yet been called. This clearly isn't the case when I can make my phone vibrate from both the lock screen and the password screen."

onResume is called before you unlock the phone


The following code will handle Screen Off and Screen On intents. Perhaps you can call onPause() in the onResume() called only after the screen turns on, and call and onResume() when your activity has focus again.

public class ExampleActivity extends Activity {

   @Override
    protected void onCreate() {
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        BroadcastReceiver mReceiver = new ScreenReceiver();
        registerReceiver(mReceiver, filter);
    }

    @Override
    protected void onPause() {
        if (ScreenReceiver.wasScreenOn) {
            System.out.println("onPause() called because screen turned off.");
        } else {
            System.out.println("normal onPause() call");
        }
        super.onPause();
    }

    @Override
    protected void onResume() {
        if (!ScreenReceiver.wasScreenOn) {
            System.out.println("onResume() called when screen turns on");
        } else {
            System.out.println("normal onResume() call");
        }
        super.onResume();
    }

}


Ah. Your edit tells us why this is happening.

The problem is that when your "screen times out" and the phone goes black, what is happening is the phone is actually going to sleep. The OS is probably going into a low power state and turning of listeners for various things and possibly turning off the accelerometer and/or vibrator.

If you really want the activity to be able to vibrate, try holding a wake lock.

http://developer.android.com/reference/android/os/PowerManager.WakeLock.html

0

精彩评论

暂无评论...
验证码 换一张
取 消