Skip to content Skip to sidebar Skip to footer

Schedule Two Tasks Subsequently In Android

i want to perform 2 tasks. First should repeat once in every 10min Second should repeat every minute. Example Opening a website in first task Opening another website in second task

Solution 1:

For the scheduling part you can use the AlarmManager

For instance:

publicclassTaskScheduler {
    publicstaticvoidstartScheduling(Context context) {

            Intentintent=newIntent(context, MyReceiver.class);
            PendingIntentpendingIntent= PendingIntent.getBroadcast(context, 0, intent, 0);
            AlarmManageralarmManager= (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 600, pendingIntent);

    }
}

Then inside your receiver class you can start an IntentService:

publicclassMyReceiverextendsBroadcastReceiver {    
    @OverridepublicvoidonReceive(Context context, Intent intent) {
        IntentintentService=newIntent(context, MyService.class);
        context.startService(intentService);
    }
}

MyServicelooks roughly like:

classMyServiceextendsIntentService {
    publicMyService() { 
        super(MyService.class.getSimpleName());
    }

    @OverridepublicvoidonHandleIntent(Intent intent) {
        // your code goes here
    }
}

And finally, don't forget to register MyReceiver in the manifest file:

<receiverandroid:name="Your.Package.MyReceiver"></receiver>

As well as your service:

<serviceandroid:name="..."></service>

Solution 2:

Check AsyncTask, there is explanation and example here: http://developer.android.com/reference/android/os/AsyncTask.html

Post a Comment for "Schedule Two Tasks Subsequently In Android"