Mark:

Oh, so I figured it out.  There is some bug in the emulator I guess.  I ran
into it once before and at the time, just assumed I had done something
wrong.

Basically, I created a new project from your sample Alarm service and ran it
to see if it would work on my emulator.  Not only did it work, so did mine!

This happened before when I was having trouble w/ receivers, alarms,
services, etc.  It's like I have to build and install another totally
innocuous and unrelated project and then all of a sudden mine will start
working.  But the alarm is only received and service started when I run the
other project in the emulator!

Thanks,
Nick Owens
VP, ThreeClix
Office: (904) 429-7039
Mobile: (847) 565-9392
After Hours: (904) 540-5830


-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Mark Murphy
Sent: Sunday, February 28, 2010 1:38 PM
To: [email protected]
Subject: Re: [android-developers] AlarmManager Not Firing Receiver

Nick Owens wrote:
> I took some excerpts from your fabulous sample online.  I have never
worked
> w/ a WakefulIntentService before and am finding it hard to follow.

Well, it's covered in (*ahem*) one of my books.

:-)

> What is calling the doWakefulWork(Intent) function?

WakefulIntentService does. Going back up the chain, your call to
startService() will trigger your service to be called with onStart().
IntentService implements onStart() and queues up the Intent to be
processed on a background thread. That thread passes it to
onHandleIntent(). WakefulIntentService implements onHandleIntent() to
call doWakefulWork() and then release the WakeLock.

> The AlarmManager is set to fire OnAlarmReceiver and passes a special
Intent
> to it w/ an attached bundle.  I need some info from that bundle, and
> although the doWakefulWork() function takes an intent as an argument, that
> Intent does not appear to be the one that has the Bundle that was
originally
> set.

It should be. Or, rather, it should be a functional copy of that Intent.
Intents are designed to go across process boundaries, so it may or may
not be the exact same instance.

> Intent i = new Intent(context, OnAlarmReceiver.class);
> Bundle b = new Bundle();
> b.putLong("res_id", res_id);
> i.putExtras(b);
> 
> PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
> (int)res_id, i, 0);
> 
> AlarmManager alarmManager = (AlarmManager)
> context.getSystemService(Context.ALARM_SERVICE);
> alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
> (System.currentTimeMillis() + (5 * 1000)), (res_check_frequency * 60000),
> pendingIntent);

If you are going to attach extras on Intents that go into
PendingIntents, you need to take special care, perhaps using FLAG_ONE_SHOT.

> And this is the OnAlarmReceiver class:
> 
>
----------------------------------------------------------------------------
> ----public class OnAlarmReceiver extends BroadcastReceiver {
>       
>       @Override
>       public void onReceive(Context context, Intent intent) {
>               
>               WakefulIntentService.acquireStaticLock(context);
> 
>               context.startService(new Intent(context, AppService.class));
> 
>       }
>       
> }

Note that you have no extras in the Intent you are creating here.

>
----------------------------------------------------------------------------
> ----  public AppService() {
>               
>               super("AppService");
>               
>       }
>       
>       @Override
>       protected void doWakefulWork(Intent intent) {
>               
>               Log.d(TAG, "Fired alarm");
>               
>               Bundle b = intent.getExtras();
>         
>         if (b != null) {
> 
>               long res_id = b.getLong("res_id");
>               
>               Log.d(TAG, "Resource ID: " + Long.toString(res_id));
>               
>         }
> 
>       }

Since you did not supply any extras to the Intent in startService(), you
will not get any extras in the Intent in doWakefulWork().

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy

Android App Developer Books: http://commonsware.com/books

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

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

Reply via email to