Hi Mark,

After doing some more testing, I observed that its still happening that the
background service sometime runs and sometime doesn't run. However this
time, the number of time the service gets run has increased :)

public class BootCompletedIntentReceiver extends IntentReceiver {
  /* the intent source*/
  static final String ACTION = "android.intent.action.BOOT_COMPLETED";
  private static final String LOG_TAG = "BootCompletedIntentReceiver";
  @Override
  public void onReceiveIntent(Context context, Intent intent) {
    if (intent.getAction().equals(ACTION))
    {
      // Once we receive the BOOT_COMPLETED intent, we will start a service
that
      // will start a service which in turn will run alarm manager
      Intent i = new Intent();
      i.setClassName("com.smartaddress",
"com.smartaddress.AddressLogService");
      Bundle b = new Bundle();
      ComponentName cname = context.startService( i,b );
      if( cname == null )
        Log.e( LOG_TAG,"AddressLogService was not started" );
      else
        Log.d( LOG_TAG,"AddressLogService started" );
    }
  }
}

public class AddressLogService extends Service {
  private static final String LOG_TAG = "BackgroundService";
  @Override
  protected void onStart(int startId, Bundle arguments) {
    super.onStart( startId, arguments );
    Log.d( LOG_TAG, "onStart" );
    Intent i = new Intent();
    i.setClassName("com.smartaddress",
"com.smartaddress.AddressLoggerIntent");
    long firstTime = SystemClock.elapsedRealtime();
    // Schedule the alarm!
    AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
    am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                    firstTime, 10 *1000, i);
  }

  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }
}

public class AddressLoggerIntent extends IntentReceiver
{
    private static final String LOG_TAG = "AddressLoggerIntent";
    @Override
    public void onReceiveIntent(Context context, Intent intent)
    {
        Log.v(LOG_TAG, "AddressLogger Service Started");
        // Logic
  }
}

Any idea why it is happening now ? I am confused and a little frustated now
with this random behavior.

Thanks,
Jaikishan
On Mon, Jul 14, 2008 at 11:58 PM, Jaikishan Jalan <[EMAIL PROTECTED]> wrote:

> Super! Thanks Mark, your suggestion helped me to reduce the redudancy of
> my code. Now my code works perfectly fine.
>
> Thanks,
> Jaikishan
>
>
> On Mon, Jul 14, 2008 at 5:49 PM, Mark Murphy <[EMAIL PROTECTED]>
> wrote:
>
>>
>> Jaikishan Jalan wrote:
>> > Alright. I guess I jumped in too quickly to shot out my problem . I
>> > figured out a way (of course with the help of all you beautiful people
>> > :) ) to run my service in background. What I have done is I first call a
>> > service from my intent receiver. This service in turns creates an
>> > instance of Alarm Manager. Since Alarm Manager broadcasts an intent, I
>> > have another intent receiver, which receives this intent and run a
>> > service which will do run my logic. It seems to work for me but not sure
>> > if this is the most efficient and recommendable way to do this.
>>
>> As hackbod indicated, the next SDK may allow you to drop the second
>> IntentReceiver outright, due to improvements in AlarmManager.
>>
>> In the interim...you seem to have twice as many IntentReceivers and
>> Services than would seem necessary.
>>
>> If this were my app, I would aim for:
>>
>> -- IntentReceiver (A) receives the BOOT_COMPLETED Intent and uses that
>> to start the service
>>
>> -- Service (B), on startup, registers an inner class IntentReceiver via
>> registerReceiver() for my private Intent, then sets up AlarmManager to
>> raise that Intent every N minutes, with the inner class IntentReceiver
>> doing the desired work.
>>
>> Then, if that worked, I'd try to get rid of (A) by registering the
>> BOOT_COMPLETED intent-filter on (B), to see if I can knock this down to
>> a single service class (plus an inner IntentReceiver class).
>>
>> --
>> Mark Murphy (a Commons Guy)
>> http://commonsware.com
>> Android Training on the Ranch in September! http://www.bignerdranch.com
>>
>> >>
>>
>
>
> --
> Thanks,
> Jaikishan
>



-- 
Thanks,
Jaikishan

--~--~---------~--~----~------------~-------~--~----~
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]
Announcing the new M5 SDK!
http://android-developers.blogspot.com/2008/02/android-sdk-m5-rc14-now-available.html
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to