0

In my app there is a Broadcast Receiver which is called by an alarm with RTC_WAKEUP at specific times. This receiver first acquire a partial lock so it doesn't terminate and then checks a condition and if it's true start an activity which turns the phone completely on by a acquiring a full lock and unlocking keyguard.

After a little test and trial I found out lifecycle of Broadcast receiver ends before starting the activity, and as soon as it ends the partial lock is released.

  • So I wonder if the device is sleep, would the activity get started so it can acquire full lock?

  • Or as soon as the broadcast receiver ends the device goes back to sleep and start of activity is postponed to when it wakes again?

Ali
  • 21,572
  • 15
  • 83
  • 95
  • I think your app is designed to start an Activity at a specific time, like the native alarm Activity, isn't it? – Huang Dec 30 '12 at 15:03
  • @Huang Yes, it's a reminder but I want to check simple offline condition before starting the activity. Is there any source code like alarm activity available to check? I couldn't find one. I want to see if I do things right. – Ali Dec 30 '12 at 15:04

2 Answers2

2
  1. If you want to start an Activity in a BroadcastReceiver, don't forget to add the flag: FLAG_ACTIVITY_NEW_TASK

     public void onReceive(Context context, Intent i) {
      if (check condition) {
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         intent.setClass(context,THE_ACTIVTY_YOU_WANT_TO_START.class);
        context.startActivity(intent);
            } 
    
     }
    
  2. use the FULL_WAKE_LOCK to force to turn the screen on, add the codes below in the onCreate() method to do so.

    pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    
        km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        kl = km.newKeyguardLock("info");
        wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
                | PowerManager.ACQUIRE_CAUSES_WAKEUP
                | PowerManager.ON_AFTER_RELEASE, "info");
        kl.disableKeyguard();
    
Huang
  • 4,812
  • 3
  • 21
  • 20
  • This is what I have done, my question is if the activity starts at all or not? – Ali Dec 30 '12 at 15:16
  • Somewhere I read disabling keyguard must be done before power wakelock and somewhere else the opposite, which one is better? – Ali Dec 30 '12 at 15:19
  • @Ali I have written an Alarm like activity, and I used the code above( calling disableKeyguard() first in onCreate() and then wl.acquire() in the onResume()). It works fine for me. – Huang Dec 30 '12 at 15:22
  • @Ali I didn't read the source code the the powermanager of AOSP and I gave my answer just based on my practice. I can't answer the mechanism behind it... – Huang Dec 30 '12 at 15:25
  • There is an strange behavior about keyguard unlocking. After unlock the onresume is first called then onpause is called, then again onresume is called. Do you know anything about this? – Ali Dec 30 '12 at 15:28
  • @Ali Yes, I've met the same problem. and I even asked such a question here. http://stackoverflow.com/questions/6715653/strange-lifecycle-behavior-in-my-screen-saver-app One answer say that it results from a transition animation when the device gets unlocked, but I'm not sure... I didn't study the source code. But in general, that won't hurt you if your app is just a reminder. call wl.acquire in onResume() and wl.release() in onPause() – Huang Dec 30 '12 at 15:32
  • There is another bug there, I finish activity when audio ends, and there is a conflict between finish and release lock which says Program stopped unexpectedly, even no exception!? – Ali Dec 30 '12 at 16:02
0

While looking through the GCM manual I found the following chapter:

http://developer.android.com/google/gcm/gcm.html#handling_intents

I think you have to aquire the WakeLock while still being in BroadcastReceiver#onReceive. They use a static WakeLock in their IntentService.

j0k
  • 22,600
  • 28
  • 79
  • 90