Start app at a specific time

0 votes
asked Dec 15, 2010 by tiger

I was wondering if it's possible (and if it is how) to start up my app at a specific time, something like an alarmclock which goes off at a specific time. Let's say I want my app to start up at 8 in the morning, is that feasable ?

2 Answers

0 votes
answered Dec 15, 2010 by jeffrey-blattman

You are probably looking for AlarmManager, which let's you start services / activities / send broadcasts at specific intervals or a given time, repeating or not. This is how you write memory friendly background services in android. AlarmManager is sort of like cron in unix. It allows your background service to start, do its work, and get out of memory.

You probably do not want to start an activity (if that's what you meant by "application"). If you want to alert the user that something has happened, add an alarm that starts a receiver at a given time, and have the receiver add a notification. The notification can open the application when clicked. That's less invasive than bringing some potentially unwanted activity to the foreground.

0 votes
answered Dec 15, 2010 by ninjasense

You can do it with AlarmManager, heres a short example. First you need to set the alarm:

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

Date futureDate = new Date(new Date().getTime() + 86400000);
Intent intent = new Intent(con, MyAppReciever.class);

PendingIntent sender = PendingIntent.getBroadcast(con, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, futureDate.getTimeInMillis(), sender); 

Next, You need to create a reciever with some code to execute your application: (ie- starting your app):

public class MyAppReciever extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {

    startActivity(new Intent(context, MyAppMainActivity.class));
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter