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

Reply via email to