Hi Mark,

I have the wake lock defined as a static member of the broadcast
receiver.  I think I lifted a bunch of code from an article that you
wrote, so I am probably not needing the service.  I guess I could just
launch the application directly.  Declaring the wake lock as a static
member of the alarm receiver should be sufficient though, eh?

On Feb 11, 10:01 am, "Mark Murphy" <mmur...@commonsware.com> wrote:
> > I am setting an alarm like thus:
>
> >     AlarmManager mgr = (AlarmManager)
> > activity.getSystemService(Context.ALARM_SERVICE);
> >     mgr.setRepeating( AlarmManager.RTC_WAKEUP, msecs, INTERVAL_MSECS,
> > pendingIntent );
>
> > to go off at 3am in the morning.
>
> > the alarm gets caught by a broadcast receiver.  in the onReceive
> > method of the broadcast receiver, I am acquiring a wakelock and
> > starting a service.
>
> >     @Override
> >     public void onReceive(Context context, Intent intent) {
> >         PowerManager mgr = (PowerManager)
> > context.getSystemService(Context.POWER_SERVICE);
> >         lock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
> > WAKELOCK_NAME);
> >         ...
> >         context.startService(new Intent(context, MyService.class));
>
> Depending on where 'lock' is defined and when you're releasing the lock,
> that may not work.
>
> > in my service I am trying to start my app --
> >     @Override
> >     public void onStart(Intent intent, final int startId) {
> >         super.onStart(intent, startId);
> >         Intent intentStartMyActivity = new Intent(Intent.ACTION_MAIN);
> >         intentStartMyActivity.putExtra("com.test.main", true);
> >         startActivity( intentStartMyActivity );
>
> Why not just start the activity from the BroadcastReceiver? What's the
> point of the service?
>
> And I really hope that this is either an alarm clock app or you make this
> configurable, as users may not appreciate an activity spontaneously
> starting up triggered by some background operation.
>
> > I have some additional logging my start activity and discovered that
> > sometimes this works and sometimes it doesn't.  It will work like 4
> > nights in a row and then on the 5th night, it won't work, and then it
> > will work three more times after that.  Any ideas on why it might
> > 'sometimes' not work?
>
> WakeLocks are tricky beasts. I spend a few pages in a book trying to
> explain how to handle this pattern well. I can't tell whether your code is
> OK, because it's missing some key pieces, like where 'lock' is declared
> and when you're releasing the WakeLock.
>
> I have the pattern wrapped up in a reusable component, though it is very
> much not designed for use with an activity:
>
> http://github.com/commonsguy/cwac-wakeful
>
> You could take a peek at the code (it's not that big) and example to see
> how to apply it in your app.
>
> --
> Mark Murphy (a Commons Guy)http://commonsware.com
> Android App Developer Books:http://commonsware.com/books.html

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to