Executing code when Application is updated/reinstalled - android

I have Some Alarms set via AlarmManager to do some periodic jobs.
I set/reset them when user opens the app for the first time & on every boot_complete event.
But when the app is reinstalled via ADB then my alarms do not fire anymore. looks like the OS deletes the Alarms on reinstall of the app. I assume this will happen if the user updates the app from the market also.
If I can receive a broadcast or some sort of callback in the event of reinstall/upgrade etc of my application, I can set the alarms again. but i don't know if it is possible or how?
Can someone please help me out.

Yes, this is possible.
You can create a broadcast receiver that listens for any PACKAGE_* events the system sends, but you won't receive them for your own application except for when your application is being upgraded — you'll get PACKAGE_REMOVED followed soon after by PACKAGE_REPLACED.

Related

Offline reminder notification app (Google Keep like) for Android 11 target. How to receive notifications when app is closed?

I searched a lot, but couldn't find nothing clear. Every solution seems outdated. In my app, I need this simple task: the user can enable a daily reminder and select a time in the Time Picker.
Everyday in the selected hour the user should receive a simple notification. I only need to read the local database (Room, SQLite) and then show a notification.
I'm using AlarmManager, because the user should receive the notification in the exact time he selected.
Everthing is working fine when the app is open or minimized. When I close the app in the recently used apps (swipe up) I don't receive the notification.
I have a class that creates the alarm with this calling:
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingIntent);
The pendingIntent opens a NotificationReceiver extends BroadcastReceiver class. In the onReceive method I read the db and show the notification. I also tried starting a service in the onReceive method (IntentService or Service) but they work exactly as just using the broadcast receiver.
I put the receiver in the AndroidManifest. Everything is working except when I close the app or restart the system (I registred the BOOT_COMPLETED in the manifest and I have another broadcast receiver that configure the alarm again. If I open the app and keep it open before the notification time, then I receive it. But if the app is still closed, I don't receive it. So I thik the BOOT_COMPLETED is working fine).
The other solutions doesn't seem to work. What can I do to have it working on Android 11 (Pixel 3 device)?
One example of what I need is the Google Keep app, but mine is simpler. I don't see any notification dot telling that the Google Keep is running in background. Still, I get the reminders on time. How the Google Keep works? It's a service always running (and killing the battery)? It uses AlarmManager and BroadcastReceiver? WorkManager? Even if a close the app or restart the phone, I always get the notifications. How can I achieve it on my app?

Boot receiver without need to restart device

I have a receiver which fires when boot is completes, using inter filter android.intent.action.BOOT_COMPLETED.
The problem is, when the user installs the app, i want this event to be fired. Without the user having to reboot the device. What is the best way to do this?
You cannot do this. In fact, the user must start your app manually at least 1 time before BOOT_COMPLETED will ever get delivered to your app. This is done by the framework to prevent apps from installing things which sit in the background without the user's knowledge.

Android Alarm Manager not being triggered

I have my app installed in 2000 devices. In most of them it is working great. But some users (maybe 100) report me that the app is not updating correctly.
I found out that the problem is that the AlarmManager is not triggering an Alarm. The app is supposed to update data every 15 minutes so I use an Alarm Manager for that.
With no reason, it seems like it stops working after a while of running and it never starts again unless the phone is rebooted. Then it works again for a while and finishes stopping, too.
I don't have access to those devices but the users tell me they don't have any task manager or background cleaners. And in case they have them, then my app is in the 'ignore list'.
I have tried to emulate this scenario but I cannot.
It seems like the app is killed and the alarm managers don't work. By the way, those killed apps don't receive GCM events either.
So the questions are:
Is there anyway to prevent the app from being killed or to ensure the alarm is triggered at the time I set.
Can I use broadcast events like Screen-on, Screen-Off or Telephony incoming events to check if the alarm has been triggered and in case it is not to do it at that moment? I am not sure if this would work because if my app is killed then probably it will not receive any event, correct?

How to send local notification android when app is closed?

I'd like to know if it's possible to send a local notification to the device when the app have been opened then closed.
It works already when my app is opened and when it's in the background.
Thanks
Edit : I think i wasn't clear enough:
I want to send a local notification at a given time even if the app is not running at that time.
By "Local Notification" i mean a Notification (android class) created in my app and send by using an AlarmManager and a BroadcastReceiver.
closed -> killed the process of my app
Most means of "killed the process of [your] app" will leave your alarms intact, and so whatever you have scheduled will remain scheduled and keep being invoked as you set up.
I want to send a local notification at a given time even if the app is not running at that time.
Again, AlarmManager is not dependent upon your process being around, so long as it can invoke the PendingIntent that you supply. So long as the BroadcastReceiver you are using is registered in the manifest (and not via registerReceiver()), it should work fine.
If the user force-stops your app -- usually via the Settings app -- then not only will your alarms not be invoked, but your code will never run again, until something explicitly starts up your app (usually the user tapping on your icon in the launcher). There is nothing that you can do about this.
it's hard to explain things when you do understand fully and when english is not your native language
There are many Android developer support sites, offering a variety of languages.
you need to create a service, http://developer.android.com/guide/components/services.html

Android BOOT_COMPLETED

I have a question concerning the BOOT_COMPLETED event.
I need a service to be running at all time (done via AlarmManager) but I wonder if I have to start the service manually the first time the application is installed as the BOOT_COMPLETED event is sent only after the device is restarted. How is this commonly handled, it seems like no one is having this problem, am I getting something wrong here?
the user should be the one deciding on if the service is running or not when its first installed and not you, so yes it should be started manually when they launch the app for the first time

Categories

Resources