I have a alarm receiver start a service for every 5 mins. In the service Iam synching my database with a server. if the server splits the packets and send. I need to send the request again with a session id. The problem is when i do that i want the alarmreciever to wait until the service finish its work. ie only when i get all the packets the next 5mins should be counted. my alarm receiver:
public class OnBootReceiver extends BroadcastReceiver {
private static final int PERIOD = 60000; // 1 minute 300000 for 5 min
@Override
public void onReceive(Context context, Intent intent) {
AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(context, OnAlarmReceiver.class);
PendingIntent pi=PendingIntent.getBroadcast(context, 0,i, 0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime()+60000,PERIOD,pi);
}
}
and my Appservice
public class AppService extends WakefulIntentService {
public AppService() {
super("AppService");
}
@Override
protected void doWakefulWork(Intent intent) {
//connecting to server and getting data here.
Log.v(null, "wake up");
...
while (syncstaus.equals("more"){
// send request again
}
}
}
only after the loop finishes i want the alarm to start again. how can i do this. please throw some light on how this works
Thanks in advance
Don’t set your alarm repeating.
Consider two options :
1) the service itself, after its job could ask the alarm manager to set a new alarm at fixed time
2) use handler.postDelayed to do that instead of alarm manager. I find more flexible.
Regards,
Stéphane