user2607848
user2607848

Reputation:

launching a method after a certain time

i'm trying to lunch a method from alarm manager in the same class. Well, in my service class i have this method:

new loadSomeStuff().execute();

How can i launch it after 3 hours using alarm manager?

public class MyIntentService extends Service {
    SharedPreferences prefs;
    public static String prefName = "SecretFile";
    public static String version = "56";
    public final static int uniqueID = 1394885;

    public void onCreate() {
        super.onCreate();
        //i want after the alarm manager stop to launch this method.
        new loadSomeStuff().execute();
    }

Upvotes: 0

Views: 140

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006869

Step #1: Create a PendingIntent that will launch your service

Step #2: Get an AlarmManager by calling getSystemService() on some Context, such as your activity

Step #3: Call set() on AlarmManager to schedule your PendingIntent to run in three hours

However:

  • Please consider using an IntentService if possible, rather than a Service with what would appear to be an AsyncTask

  • If you are anticipating this work to be done even if the device is in sleep mode in three hours, you will need to use a _WAKEUP-style alarm and something to keep the device awake while the work is going on, such as WakefulBroadcastReceiver or WakefulIntentService

Upvotes: 2

Related Questions