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.
- The function should be executed at exactly 20pm.
- #1 should be repeated everyday.
- The notification should be pushed even if the app is closed.
- 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
How to Implement Placeautocompletefragment and Placeautocompleteactivity to Get Place Details
How to Dismiss Alertdialog in Android
Android 1.5: Reading Sms Messages
What's the "Dot" for When Registering an Activity
Cannot Lower Case Button Text in Android Studio
Android Device Chooser -- Device Not Showing Up
How to Build a Native (Command Line) Executable to Run on Android
How to Implement Export SQLite to Excel/CSV File in Android
How to Change Viewpager's Page
Getting Certificate Details from an APK
Detect Back Button But Don't Dismiss Dialogfragment
How to Use Google Login API with Cordova/Phonegap
Android Finish() Method Doesn't Clear App from Memory
Justify Text in an Android App Using a Webview But Presenting a Textview-Like Interface
Android Ndk C++ Jni (No Implementation Found for Native...)