Reputation: 19446
I have found some interesting behavior and do not know why or how to get around it. The activity is a singletask activity, which means onNewIntent() is for the activity while the activity is on the top of the stack. This works fine and well, while the phone's screen is on. Once the screen is off however, the new intent is not received until the phone is "awake" and at that point the onNewIntent() is called. If the activity is not at the top of the stack and the phone is asleep then the activity is started and the onCreate() method is called.
The Activity declared as:
<activity android:name=".MyActivity"
android:launchMode="singleTask"
android:alwaysRetainTaskState="true" >
The Activity is also launched with the FLAG_ACTIVITY_NEW_TASK flag. The intent is launched from a broadcast receiver. If the phone is asleep and the intent is broadcast the activity wakes the phone up with these flags:
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
However the activity is not created until the phone is awake when it is on the top of the stack. Right now I am perplexed and not sure where to start. Also my activity uses onSaveInstanceState(). The activity is a FragmentActivity and contains one fragment.
Any help would be greatly appreciated!
Upvotes: 3
Views: 1244
Reputation: 22637
However the activity is not created until the phone is awake when it is on the top of the stack.
As far as i know, that's by design. The only way to wake up the phone and keep it awake is by grabbing a wake lock.
http://developer.android.com/reference/android/os/PowerManager.WakeLock.html
The typical pattern is that you receive some intent to wake up your app, like the network state has changed, or whatever. In your receiver, in onHandleIntent(),
you grab a wake lock. You start a service to do some work, and the service releases the wake lock after it is done.
In your case, the receiver that sends the intent to your activity can grab a partial wake lock, then your activity can grab a screen wake lock. Keep in mind that as long as you hold the wake lock, the phone cannot sleep ... causing it to use much more battery. Wake locks are very dangerous things for that reason.
Note that you must do the lock hand off. If you don't hold a wake lock when onHandleIntent()
returns, the device can go right back to sleep. So it'd be something like,
receiver: acquire partial lock
activity: acquire screen lock
activity: release partial lock
You could also probably just grab the screen lock temporarily, then release it ... the screen would come on, then go off normally according to the display timeout setting.
Upvotes: 2