Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How can I wake up the phone from a hard sleep to take a picture?

I want to take pictures from the Android device's camera periodically over a matter of hours, to create a time lapse video effect.

I set an Alarm Manager with an AlarmManager.RTC_WAKEUP flag set to start up a service every few minutes.

The service holds a partial wakelock, does some work, and then calls a Broadcast Receiver through the Alarm Manager which starts up an Activity.

The activity is created (or is resumed), turns on it's own wakelock, and sets up the camera preview surface. Once the surface is setup the SurfaceHolder listener's surfaceChanged() method is called, which finally takes a picture.

If the device is awake, everything works perfectly as expected. But if the device is asleep, once the Activity's onResume() method is finished the Activity is instantly paused. The camera's preview surface never finishes initializing, and no picture will ever be taken.

So the questions I have are:

  1. Is there any way to wake up the phone programmatically? I even try using:

    PowerManager powerManager =
                (PowerManager)this.getSystemService(Context.POWER_SERVICE);
    powerManager.userActivity(SystemClock.currentThreadTimeMillis(),false);
    

But that doesn't wake up the phone if it is asleep.

  1. Is there any way to take a picture without using a preview surface view?

  2. Is there a way to take a picture that doesn't rely on asynchronous callbacks? Can I put all the code in the Activities onResume() method to take a picture?

  3. Is there any way to keep the Activity's onResume() method running long enough so that the camera's preview has enough time to initialize and call all the listeners?

I am using the wakelocks correctly, and I have all the permission's set properly in the manifest file. My activity isn't kept awake long enough for the asynchronous listeners to properly work.

And to compound the issue, I'm trying to keep everything Android 1.6 compatible, because that is the only test device I have access to.

This is frustrating stuff!

like image 521
Paul Klemstine Avatar asked Mar 07 '11 02:03

Paul Klemstine


People also ask

How do I wake-up my Android phone from sleep mode?

Turn on Lift to wake For an effortless way to activate this, just set the screen to turn on when you pick up the phone. From Settings, search for and select Lift to wake. Tap the switch next to Lift to wake to turn this feature on.

How come when I wake-up before my alarm?

Why you're waking up before your alarm. You can thank your body's finely tuned internal clock for the early wake-up call, according to a study published in Science. It's all courtesy of the little-known KDM5A gene, dubbed “the alarm clock gene.” How does this tiny gene have such a powerful effect?


2 Answers

I have finally gotten somewhere now.

I have to create a wakelock using these two flags

PowerManager.SCREEN_BRIGHT_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP, "bbbb");
wl.acquire();

Then the device wakes up, and starts at the keyguard screen.

But the only way I can get past the keyguard screen and take a picture is to use these flags on the Activity's window:

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
    | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

But this is only available with Android 2.0, and doesn't work in 1.6.

like image 125
Paul Klemstine Avatar answered Oct 08 '22 08:10

Paul Klemstine


You can also disable the Keyguard screen with

KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
km.newKeyguardLock(TAG).disableKeyguard();

provided you have the DISABLE_KEYGUARD permission.

That's available since API Level 1.

like image 20
c2u Avatar answered Oct 08 '22 08:10

c2u