Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start app at a specific time

Tags:

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 ?

like image 667
TiGer Avatar asked Dec 15 '10 16:12

TiGer


People also ask

How do you make an app open at a certain time?

Starting application on a schedule can also be achieved by AutomateIt app. Timer can be set and the app you choose will launch at a specific time.

How do you start an activity only once the app is opened for the first time?

It is important to check that the first activity which opens when the app is launched is MainActivity. java (The activity which we want to appear only once). For this, open the AndroidManifest. xml file and ensure that we have the intent-filter tag inside the activity tag that should appear just once.

How do I reduce the startup time of an app?

Load only the data necessary for rendering the first frame, and delay the rest until after launch. Refreshing data, for instance, may be possible to defer until after the app has launched. Use placeholder content for items not critical for launch: Retrieve only the data necessary to display the app's initial view.


2 Answers

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); futureDate.setHours(8); futureDate.setMinutes(0); futureDate.setSeconds(0); 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 {  @Override public void onReceive(Context context, Intent intent) {      startActivity(new Intent(context, MyAppMainActivity.class));    } } 
like image 192
ninjasense Avatar answered Sep 18 '22 14:09

ninjasense


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.

like image 26
Jeffrey Blattman Avatar answered Sep 21 '22 14:09

Jeffrey Blattman