András Ujvári András Ujvári - 8 months ago 56
Android Question

Android: frequently http requests

What is the easiest way to make frequently (45 min) a http request?
(I dont want to use any push up notifications)
I already tried AlarmManager, but it didn't work.
Forground Service can maybe be good, but may it uses to much battery.
Thank you for the answer!


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.