I want to re-start my app through Pending intent. Below code is not working.
val intent = Intent(this, Activity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
}
val pendingIntentId = 1
val pendingIntent = PendingIntent.getActivity(this, pendingIntentId, intent, PendingIntent.FLAG_CANCEL_CURRENT)
val mgr = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val timeToStart = System.currentTimeMillis() + 1000L
mgr.set(AlarmManager.RTC, timeToStart, pendingIntent)
exitProcess(0)
Target version is 31, so updated pending intent with PendingIntent.FLAG_MUTABLE still not working. I searched in many links related to this but no luck.
Restarting Android app programmatically
Force application to restart on first activity
https://www.folkstalk.com/tech/restart-application-programmatically-android-with-code-examples/#:~:text=How%20do%20I%20programmatically%20restart,finishes%20and%20automatically%20relaunches%20us.%20%7D
In Nov 2022, When target version is 31 & min sdk version is 29, above pending intent code is not restarting the App.
Any clue why above pending intent is not working or any other suggestion apart from re-launching the activity ?? I don't want to re-launch using startActivity(intent)
When you want to resteart the entire app you could use the very easy libary: ProcessPhoenix
You can just simply insert the Library and execute:
ProcessPhoenix.triggerRebirth(context);
or with a specific intent:
Intent nextIntent = //...
ProcessPhoenix.triggerRebirth(context, nextIntent);
This is the easiest way to restart an android app programatically.
Related
I have been coding an app on my phone, a xiaomi with MIUI 12.0.3 and android 10, and the alarm triggered normally, but recently I changed to another phone, a realme with android 12, and the alarm doesn't trigger anymore.
Instead of the application opening itself when the alarm triggers and the new activity starting, I need to open the app and then the activity starts, the alarm never opens the app by its own, which is what I am looking for since this is essentialy an alarm app.
I tried changing all the battery settings and background restrictions on the phone but still it doesn't work. Is there something that have changed on the android version that has made this stop working? Here's the code of the alarm.
#RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private fun prepareNextAlarm() {
alarm = getSystemService(ALARM_SERVICE) as AlarmManager
val intent = Intent(applicationContext, Pedometer::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pi = PendingIntent.getActivity(applicationContext, 0, intent, 0)
val nextAlarmTime = getNextAlarmTimestamp();
val alarmInfo = AlarmManager.AlarmClockInfo(nextAlarmTime, pi);
saveNextAlarmTime(nextAlarmTime)
alarm!!.setAlarmClock(alarmInfo, pi);
}
Thank you so much for everyone in advance for the help!
I'm creating an activity logging app, Where it's user can explicitly set a series of time first. On those set times I want to launch one of the activity which need to popup & collect data from user and go away.
(Data like how much you drink today, How many km jogged, What foods you ate, Did you exercise today etc...).
These are the constrains
The app is very much dependent on precise user defined dates and times. So I think I
had to use setExactAndAllowWhileIdle() with AlarmManager
User can be fully aware that the app need to run always. I've no issues running my tracker as a HIGH_PRIORITY Foreground service if that can offer reliable explicit data collection from user. In that case how to change this code?
For this purpose, I tried using AlarmManager. But it is triggering only when the app is in forground. How can I call the activity even if the app is not running on precise timings?
fun setAlarm()
{
val myIntent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this, 0, myIntent, 0)
val alarmManager: AlarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val calendar: Calendar = Calendar.getInstance()
calendar.timeInMillis = System.currentTimeMillis()
calendar.add(Calendar.SECOND, 5)
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent)
Toast.makeText(baseContext, "Alarm Set", Toast.LENGTH_LONG).show()
}
Or should I use start an always running foreground service and later on set times throwing an Intent to OS for starting an activity? Is it a good approach?
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
I need help with my clock alarm app.
I have declared a Broadcast Receiver to trigger a clock alarm
Manifest.xml
<receiver android:name=".view.broadcastreceivers.ClockAlarmReceiver"/>
class ClockAlarmReceiver.kt
class ClockAlarmReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
alarm = intent?.getStringExtra(ALARM_VIEW_EXTRA)?.let { fromJson<Alarm>(it) }!!
Log.d("ClockAlarmReceiver ", "Received $alarm")
}
}
I launch the pending intent in this way
...
val alarmPendingIntent = Intent(context, ClockAlarmReceiver::class.java).let { intent ->
intent.putExtra(ALARM_VIEW_EXTRA, alarm.toJson())
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
PendingIntent.getBroadcast(
context, alarmId.toInt(), intent,
PendingIntent.FLAG_UPDATE_CURRENT
)
}
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarmManager.setExact(
AlarmManager.RTC_WAKEUP,
targetTime.timeInMillis,
alarmPendingIntent
)
...
These are my sdk configs
'compileSdk': 29,
'minSdk' : 21,
'targetSdk' : 29
It is working well on android 9.0 and 10 but In 8 and previous versions is not working when the app is killed
Thanks in advance for your help
Hey if someone is having the same issue I found 2 things:
1. the Alarmanager was the thing that never triggered when the app was closed
2. This behavior only occurs when the app is in debug, I don't know why but when I created the release version it worked well
As mention in the android developer docs, the Alarm manager will not work in doze mode or when the app is sleeping, and if you need to ensure that your work completes even when the device is idle then use - setExactAndAllowWhileIdle() instead of set() with alarm manager.
example--
(USE)
alarmMgr.setExactAndAllowWhilwIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime() + 60 * 1000, alarmIntent);
(DON'T USE)-
alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime() + 60 * 1000, alarmIntent);
How do I programmatically "clear the app data " and then “restart” an Android app?
Already tried with below links:
Restart android app after cache clear
Android restart app after clearing cache and data
How do I programmatically "restart" an Android app?
After calling .clearApplicationUserData() application is killed.
So you can have only one of those - clear app data, or restart app.
If you want just to clear DB or some files stored by app that you know of, you can do it manually, and then restart app, but if you want to completely erase all user data you need to shut app, and maybe before that inform user that he will need to manually start app again.
I don't know about "clearing app data" but you can restart the app by using this method.
private void restartApp() {
Intent intent = new Intent(getApplicationContext(), YourActivityHere.class);
int mPendingIntentId = 2;
PendingIntent mPendingIntent = PendingIntent.getActivity(getApplicationContext(), mPendingIntentId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
System.exit(0);
}