Alireza milani
Alireza milani

Reputation: 69

How to run service in background every 10 minute?

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?

Upvotes: 5

Views: 29229

Answers (5)

ॐ Rakesh Kumar
ॐ Rakesh Kumar

Reputation: 1314

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

Upvotes: 0

Ichigo Kurosaki
Ichigo Kurosaki

Reputation: 3843

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

Upvotes: 5

marilion91
marilion91

Reputation: 2094

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);
    }

Upvotes: 0

Murtaza Khursheed Hussain
Murtaza Khursheed Hussain

Reputation: 15336

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
     }

Upvotes: 0

VipulKumar
VipulKumar

Reputation: 2395

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

Upvotes: 0

Related Questions