5

I want to use a service that run in background indefinitely and call a method every 10 minute and its running even app killed

How to create it?

Alireza milani
  • 69
  • 1
  • 1
  • 4
  • possible duplicate of [Android run thread in service every X seconds](http://stackoverflow.com/questions/8230606/android-run-thread-in-service-every-x-seconds) – duggu Jan 21 '15 at 08:19
  • May I ask why? The reason I'm asking is because many times in Android, you have multiple ways of doing the same thing. – Stephan Branczyk Jan 21 '15 at 08:35

5 Answers5

5

You can do by using service as follows

@Override
  public int onStartCommand(Intent intent, int flags, int startId) {

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            //performe the deskred task
        }
    }, 10minutes time in milisecods);

      // If we get killed, after returning from here, restart
      return START_STICKY;
  }

This service will get started automatically even if app get killed, and postdelayed will run

Ichigo Kurosaki
  • 3,765
  • 8
  • 41
  • 56
0

For "how to work with services", see
Services - Android
Services in Android - Vogella

Here is a clear solution that focus on "every 10 minutes" part using AlarmManager: https://stackoverflow.com/a/10222390/2591556

Community
  • 1
  • 1
VipulKumar
  • 2,385
  • 1
  • 22
  • 28
0

Assuming you have a Running Service

User AlarmManager to run Service every 10 minutes

   AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
         Intent i = new Intent(context, YourService.class);
         PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
         am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 600000, pi); // Millisec * Second * Minute
     }
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
  • `RTC_WAKEUP` doesn't work with `Service`. You would have to use `WakefulBroadcastReceiver` to do this – NinjaCoder Nov 09 '17 at 03:06
  • Kindly explain, it's for starting pending intent through alarm manager and it is verified and working. But its have been 2 years since initially posting many things came in between on Android. – Murtaza Khursheed Hussain Nov 09 '17 at 05:07
0

you could write a background Service: Running in a Background Service

and start the service every 10-11 min (cause of AlarmManager power saving behaviour), or with exact timing (needs to shedule next execution every time) with AlarmManager.setExact

Example:

private static PendingIntent createClockIntent(Context context) {
        Intent intent = new Intent(context.getString(R.string.widget_broadcast_clock_update));
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 1,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
        return pendingIntent;
    }

    public static void startClockAlarm(Context context) {
        AlarmManager alarmManager = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        clockIntent = createClockIntent(context);
        alarmManager.setRepeating(AlarmManager.RTC, 0,
                600000, clockIntent);
    }
marilion91
  • 2,094
  • 1
  • 19
  • 28
0

You can use the Alarm manager which will be called after every 10 mins

        AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        Random random = new Random();
        int m = random.nextInt(9999 - 1000) + 1000;

        Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
        notificationIntent.setClass(this,AlarmReceiver_.class);
        notificationIntent.addCategory("android.intent.category.DEFAULT");

        PendingIntent broadcast = PendingIntent.getBroadcast(YourClass.this, m, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() + 300000L,
                600000L, broadcast);

ManiFest receiver Code where you will get a receiver response

<receiver android:name="com.yourpackage.AlarmReceiver_"

        >
        <intent-filter>
            <action android:name="android.media.action.DISPLAY_NOTIFICATION" />
            <category android:name="android.intent.category.DEFAULT" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            <action android:name="android.intent.action.REBOOT" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />

        </intent-filter>
    </receiver>

You will have to create Receiver where you will receive data as an above-specified name of AlarmReceiver_.class

ॐ Rakesh Kumar
  • 1,318
  • 1
  • 14
  • 24