I am trying to start a service regularly every 30 second (even when the app is closed). but it runs one after another immediately. I started the service for the first time by:
Intent i= new Intent(this, MyService.class);
this.startService(i);
and my service is
public class MyService extends Service {
public static String READ_NEMAD_URL = "http://...";
@Override
public void onStart(Intent intent, int startId) {
ReadNemad task = new ReadNemad("GCAZ92");
task.execute();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
Calendar cal = Calendar.getInstance();
Intent intent1 = new Intent(this, MyService.class);
PendingIntent pintent = PendingIntent.getService(this, 0, intent1, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// Start every 30 seconds
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
30 * 1000, pintent);
}
private class ReadNemad extends AsyncTask<String, Void, String> {
//Do Somethings
return response;
}
@Override
public void onPostExecute(String response) {
stopSelf();
}
}
}
why the service run by itself? how can I fix it to run just every 30s?