András Ujvári András Ujvári - 1 year ago 109
Android Question

Android: Repeated HTTP requests

What is the easiest way to make a repeated HTTP request (i.e every 45 min) ?
I dont want to use any push up notifications
I've already tried AlarmManager, but it didn't work.
Forground Service can be good way, but it might may consume too much battery.

Answer Source

Call this metod from your MainActivity:

private void setNotifyAlarm() {
        long _alarm;
        Calendar now = Calendar.getInstance();
        Calendar wakeupcall = Calendar.getInstance();
        wakeupcall.set(Calendar.HOUR_OF_DAY, 15);
        wakeupcall.set(Calendar.MINUTE, 30);

        if (wakeupcall.getTimeInMillis() <= now.getTimeInMillis())
            _alarm=wakeupcall.getTimeInMillis() + (AlarmManager.INTERVAL_DAY+1);

        al = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        notif= new Intent(this,NotifyService.class);
        fintent = PendingIntent.getService(this,0,notif,0);
        al.setRepeating(AlarmManager.RTC_WAKEUP,_alarm,"45 minutes in Miliseconds", fintent);

Create Service for http requuest:

public class NotifyService extends Service {

    private NotificationManager mNotificationManager;
    PowerManager powerManager;
    PowerManager.WakeLock wakeLock;

    public void onCreate() {
        powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "NOTIFY");


    public IBinder onBind(Intent intent) {
        return null;

    private void handleSERVICE(Intent intent){
        new NotifyTASK().execute();

    private class NotifyTASK extends AsyncTask<Void, Void, Void> {

        protected Void doInBackground(Void... voids) {
            DO your HTTP CALLS

            return null;

        protected void onPostExecute(Void aVoid) {

    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;

    public void onDestroy() {

Now add this in Manifest file:

 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name=""/>
 <uses-permission android:name="android.permission.WAKE_LOCK"/>

<service android:name=".NotifyService" android:exported="true" android:enabled="true"/>

This code start service at specified time intervals. Hope this help you, if you need more code then tell me.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download