1

My app needs to do some background task after boot completed and also in sleep mode. So I'm using WakefulIntentService to avoid flow problem in sleep mode and it works well. But when I'm trying to use the boot receiver for continuing data flow after boot completed it gives some strange error and also not working sometimes.

Here is my Boot Receiver class where I am trying to do multiple task.

public class OnBootReceiver extends BroadcastReceiver {

private static final int SERVICE_PERIOD = 300000; // 5 minutes currently
// private static final int HANDLER_PERIOD = 300000 * 12; // 1 hour
// currently
private static final String TAG = "OnBoot";
private Context mContext;

private int mInterval = 1000 * 60 * 60 * 2; // 2 hours by default, can be
                                        // changed later
private Handler mHandler;

@Override
public void onReceive(Context context, Intent intent) {
    mContext = context;
    AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, OnServiceReceiver.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);

    mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + 60000, SERVICE_PERIOD, pi);

    mHandler = new Handler();   
    startRepeatingTask();

}

Runnable mStatusChecker = new Runnable() {
    @SuppressWarnings("unchecked")
    @Override
    public void run() {

        SharedPreferences mSharedPreferences = mContext
                .getSharedPreferences(KEY_USER_ID, 0);
        String UserId = mSharedPreferences.getString(KEY_USER_ID, "0");
        new SyncValidater(mContext, UserId).execute();
        mHandler.postDelayed(mStatusChecker, mInterval);
    }
};

void startRepeatingTask() {
    mStatusChecker.run();
}

void stopRepeatingTask() {
    mHandler.removeCallbacks(mStatusChecker);
}
 }

In this class I have written two tasks. the first one is starting another broadcast receiver which starts the wakeful service(same as github example). And the second one starts one handler which starts one AsyncTask class to do some server transaction in every two hours. But sometimes the handler starts in random time and sometimes stop.

So here I'm confusing that is my app requires two boot receivers ? is there any other option to do these multiple tasks in the current receiver ?

please help. Thanks

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
user3678972
  • 207
  • 2
  • 12

1 Answers1

2

And the second one starts one handler which starts one AsyncTask class to do some server transaction in every two hours

No, your second task will run once and probably never again, because your process is terminated in the meantime.

So here I'm confusing that is my app requires two boot receivers ?

No.

is there any other option to do these multiple tasks in the current receiver ?

Use two AlarmManager events, each with their own period. One is your current AlarmManager event. The other is one you create in support of your every-two-hour work to be done.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for the answer. But I have a little confusion that where i have to write the both alarmmanagers. Inside the doWakefulWork() of my service or inside the bootreceiver class. – user3678972 Jun 16 '14 at 15:00
  • @user3678972: "But I have a little confusion that where i have to write the both alarmmanagers" -- you already have one. I would assume that you should configure the second one right after you configure the first one. – CommonsWare Jun 16 '14 at 15:03