3

I know this question is asked before, but I didn't get any answer, I want to create an intent service that run a thread all the time, but when I get out from the app my service is stopped then the thread stopped too. I need to create something to wake up the service every some minutes. or something to prevent killing the service even when app is killed or closed.

this is how I start my service

Intent intent= new Intent(Intent.ACTION_SYNC,null,this,IntentServ.class);
startService(intent);
Neha Agarwal
  • 622
  • 7
  • 24
Malo
  • 1,232
  • 2
  • 17
  • 28
  • 1
    Use normal service instead of IntentService – Chandrakanth May 22 '15 at 07:08
  • 1
    AFAIK you can use one wakefulbroadcast with one handler...also alarmmanager will help you here,,,,did you try it – Ranjit May 22 '15 at 07:09
  • Normal service works for a period of time, while the intent service works for a lot of time then intent service is most useful – Malo May 22 '15 at 07:10
  • please refer....http://stackoverflow.com/questions/13820596/start-android-service-after-every-5-minutes –  May 22 '15 at 07:10
  • @Malo.. it is not like that...If you are creating another thread within your service then it will be more convenient to do long running tasks within it rather than intent service –  May 22 '15 at 07:11
  • is the normal service will not stop when the app is killed ? @Viren – Malo May 22 '15 at 07:18
  • 2
    for that you have to add START_STICKY as return statement in onStartCommand method of the service –  May 22 '15 at 07:20
  • you are right i create a service with START_STICKY in addition of alarm manager to wake it every 1 hour, i want to prevent device from sleeping now :) – Malo May 22 '15 at 08:04
  • im trying to prevent device from sleeping, did you think that the alarm manager does not sleep the device @Viren – Malo May 22 '15 at 08:33

3 Answers3

5

For this you can use the AlarmManager that could start your service for every 1 Hour. For Example :

 AlarmManager mgr = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
 Intent notificationIntent = new Intent(context,
                UpdateService.class);
 PendingIntent pendingIntent=PendingIntent.getService(context, requestCode, Intent.parseIntent(), 0);
  mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
        System.currentTimeMillis(), AlarmManager.INTERVAL_HOUR, pendingIntent);
Kartheek
  • 7,104
  • 3
  • 30
  • 44
  • thank you i create alarm manager in addition of normal service and its work now im trying to prevent device from sleeping, did you think that the alarm manager does not sleep the device ? – Malo May 22 '15 at 08:03
  • 3
    AlarmManager doesn't prevent the device from sleeping, but if you want the wake up the device when an alarm is triggered then you can use AlarmManager.RTC_WAKEUP while registering otherwise you can use AlarmManager.RTC. http://developer.android.com/reference/android/app/AlarmManager.html – Kartheek May 22 '15 at 09:10
  • why not just advocating `START_STICKY` and save resources ? – kiranpradeep May 23 '15 at 13:04
1

Use normal Service with android.app.AlarmManager.

Don't need to use WakefulBroadcastReceiver.

Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103
Injury
  • 143
  • 1
  • 1
  • 10
1

AlarmManager and PendingIntentare what you need in this case. Follow below example :

AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);

/* --- Create pending intent, which will be executed when wake-up --- */

PendingIntent operation = getUpdatePolicyOperation();
am.set(AlarmManager.RTC, alarmTime, operation); // alarm time is millisecond time in milliseconds that the alarm should go off, using the appropriate clock (depending on the alarm type).

You can read more about alarm mode in AlarmManager in Here or take a tutorial in Here

Hope it's helped.

ThaiPD
  • 3,503
  • 3
  • 30
  • 48