I can’t fully understand how to make my IntentService call every N seconds and do certain actions in my browser. Googled this code:

Calendar cal = Calendar.getInstance(); Intent intent = new Intent(this, MyService.class); PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0); AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE); alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1000 * 60, pintent); 

I call this code in the onCreate method of my main activation. But for some reason he does not work. How to implement it correctly?

Update: how correct will such a code be?

  final Handler handler = new Handler(); final Runnable refresh = new Runnable() { public void run() { mServiceIntent = new Intent(MainActiviy.this, myService.class); startService(mServiceIntent); handler.postDelayed(this, 60000); } }; handler.post(refresh); 
  • Is your service registered in the manifest? - Yuriy SPb
  • yes, if just startService (mServiceIntent); it works - Tany
  • Are you sure that you need to call Service every n-seconds, or do you need it to always work on the background and perform some function every n-seconds? - Android Android
  • I need to send a request to my server every 4 hours and in the event of a change write it to the phone database. So you are right. - Tany
  • Then call your service when you start the startService (mServiceIntent) application. In the service, register pendingIntent AlarmManager.setRepeating at intervals of 4 hours. And already in broadcastReceiver, call your function. - Android Android

0