Start App at a Specific Time

Start app at a specific time

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

Start an app daily / on a specific time

You can use an Alarm Manager for this as suggested here

Also don't forget to re-register the alarm when the app is rebooted.

Schedule Android function when App is closed at specific time

You should be concerned following tasks.

  1. The function should be executed at exactly 20pm.
  2. #1 should be repeated everyday.
  3. The notification should be pushed even if the app is closed.
  4. Above issues should not matter whether the device restarts.

The solution I found is following and that requires the app should be launched at least once.

#1~3 can be implemented by AlarmManager.
At app's first launch, call following code that registers alarmIntent.

private var alarmMgr: AlarmManager? = null
private lateinit var alarmIntent: PendingIntent

alarmMgr = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarmIntent = Intent(context, YourAlarmReceiver::class.java).let { intent ->
PendingIntent.getBroadcast(context, 0, intent, 0)
}

// Set the alarm to start at 20:00.
val calendar: Calendar = Calendar.getInstance().apply {
timeInMillis = System.currentTimeMillis()
set(Calendar.HOUR_OF_DAY, 20)
set(Calendar.MINUTE, 0)
set(Calendar.SECOND, 0)
}

// setRepeating() lets you specify a precise custom interval--in this case,
// 1 day.
alarmMgr?.setRepeating(
AlarmManager.RTC_WAKEUP,
calendar.timeInMillis,
1000 * 60 * 60 * 24,
alarmIntent
)

Here, YourAlarmReceiver's onReceive() will be called at every 20pm by alarmIntent.
So what you have to do is only calling throwNotification() inside this onReceive().

#4 is also easy, that being said, it can be implemented by listening to BOOT_COMPLETED event.

Android - Open app at specific time daily

AlarmManager won't work either. Doze mode prevents it (or rather, it will go off, but it may be as much as 15 minutes late). You have two options:

1)Don't use websockets. Use FCM. FCM allows you to send a high priority push message which will wake the device.

2)Have the user whitelist your from Doze mode. This cannot be done programaticly (the most you can do is launch the preference page to do it). This has to be done by the user by hand.

I really suggest #1 if you want an end to end system. #2 and requiring the user to do something outside the app is always a bit iffy, and many users won't do it.

How to launch my app every day at some particular time?

You should use AlarmManager

also you can refere to this thread Android: How to use AlarmManager



Related Topics



Leave a reply



Submit