I'm trying to make an app to restart from a command sent by my server, searching on the internet i find that the best way to do this is by using AlarmManager
so it starts the app at some moment.
So the code i've made this far looks like this:
val intent = Intent(this, StartActivity::class.java)
val wait = TimeUnit.SECONDS.toMillis(3)
val intentId = 123
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.putExtra(EXTRA_RESTART_ID, this.restartId)
intent.putExtra(EXTRA_COMMAND_NAME, this.restartCommandName)
val pendingIntent = PendingIntent.getActivity(this.applicationContext, intentId, intent, PendingIntent.FLAG_ONE_SHOT)
val manager: AlarmManager = this.getSystemService(Context.ALARM_SERVICE) as AlarmManager
// Set alarm manager to start after 3s
manager.set(AlarmManager.RTC, System.currentTimeMillis() + wait, pendingIntent)
// Kill application
this.finish()
exitProcess(2)
Doing this i've noticed that the app doesn't start again consistently.. sometimes it doesn't work and i have no idea why it doesn't, i have tried changing the wait time, as well as the intent id and exit code, but it just continues to be inconsistent.
Also.. checking the alarm system in shell using adb shell dumpsys alarm
doesn't show the alarm added by the application.
What could be the cause of the app not starting again sometimes?
Note: Phone is running on Android 9