Alarm set by alarm manager is cleaned after app is closed - java

I have been roaming in forums for days and all I found was same repeated responses which don't work at all.
I am trying to set an alarm for a certain time (e.g. 8PM) to send a notification. However, as soon as I force close the app the alarm is also cleared. It works fine if app is running in the foreground or minimized.
Peopled asked this in many other questions and received same questions that none of them work. I brought some of them in the following:
use service(sticky or other types)
use flag FLAG_INCLUDE_STOPPED_PACKAGES for the intent
start service again in onDestroy or onTaskRemoved
some other responses none of which seems to work.
Do you know any best practice to set some alarms to send a notification at a certain time?
it seems like this problems happens in some devices with restrictions like MIUIs.
Thanks in advance for your responses.

Related

How can I make a background service for timer without showing a notification?

I have tried many solution but useless, all say use foreground service.
is there a way to trick android or to use a proper way.
i know it's restriction from android to inform the user that an app is draining the battery without using it.
The answer is use a foreground service. You can't have a background service for more than 2 minutes on modern Android.
Now if your timer is long (say 1 minute+) an alarm instead of a timer would work better. That wouldn't require a service, its a broadcast and your app would be restarted to handle it. Of course if you go into Doze those are still limited.

Ensure that notification show up on Android even if app is not opened

I am building an app that is needed for a research experiment.
The experiment is entirely based on users responding to notifications that are shown at somehow random intervals.
What is the best way to do this so that each notification will definitely be shown to the user?
WHAT IS DONE SO FAR
I have used AlarmManager and broadcast receiver but notifications stop showing up after some time. Notification Channels (with id) are used too.
I suspect the operating system removes the app after it has stayed in the background for a while.
Can anyone help to explain the best way to get the app to show notifications regardless?
Since this is an app for research, users are well aware of any slow performances, or battery usage. The only priority is to ensure that notifications show up. Without this, the whole experiment will fail.
Any help or pointers will be great! Thanks!
I'm not sure what method you've used to show notifications so I'm giving a general guidance.
You will need to use a background service which keeps running even after the app has closed. A background service can be killed by the android OS if its running low on memory, so you'll have to give permission to your app to autostart.
If you want even more reliability, you should use a Foreground service. This way you can give high priority to your notification service.
But it would be much simpler to integrate Firebase Cloud Messaging into your app than implementing your own NotificationBuilder service.

Start countdown timer in my app which will work independently my app is opened or closed

My problem is: I need to start countdown timer in my app which will work independently my app is opened or closed ...or even if my smartphone is off.
I really wonder how does android standard clock app works. I mean how it counts time even when my phone is off? Could anybody help me with this? I will appreciate any answer!
Internal to an android device will be a clock which runs from the battery and potentially syncs with network time when it has signal.
You can use AlarmManager and PowerManager to get your code to run at specific times, even if the phone is turned off:
Sometimes, it may be necessary for your android app to complete a task
sometime in the future. In order to do this, you must schedule an
activity (can also be a service) to be run using Android’s
AlarmManager. This post will show: How to set up a receiver for the
scheduled event How to create an activity from this receiver Using the
AlarmManager and the created classes to successfully receive and
process a scheduled event
http://justcallmebrian.com/2010/04/27/using-alarmmanager-to-schedule-activities-on-android/
http://www.techrepublic.com/blog/software-engineer/use-androids-alarmmanager-to-schedule-an-event/

Notifications when the app is turned off

I wonder if it is possible to have notifications show up even if the app is turned off.
say for example I have an app and I set some kind of notification for 13.00 the next day and then turn the app off or restart my phone or something.
Is it possible to still get a notification in the notification bar with some text that starts the app or do the app need to run in the background to be able to use notifications?
if it is possible, what is the best way to do this? (get a notification at a certain time that starts the app) and if not, how do you recommend doing notifications?
The type of notifcations I want to do are reminder notifications.
thanks for all help!
A combination of AlarmManager and Service will help you to perform the task you are looking for, I had performed this thing.Check out the android tutorials for it.
What you're looking for is a background service. See this Android Services tutorial.
In the background service, your app should check periodically and when necessary, create and issue new notifications. See this Android Notifications tutorial.
The background service can be set to automatically start when the phone is restarted, and to re-issue any uncleared notification again.
Many apps such as Gmail already do this. Although you must be careful in resource usage or Android might kill your service.

Multiple Notifications from Foreground service Android

I'm working on an application where I need to show notifications 5 times a day ...from a foreground service..the purpose of using the foreground services is to be able to display notification even if the
application is closed . so I want to start the service as applications starts and schedule its notification timing accordingly I have tried using the snippet provided on android developers forum but its not working for me ... any help and sample code is highly appreciated Thanks
If the notifications are to be fired at a fixed time, you may not need a foreground service. It is a bad idea to keep a service running just to show notifications. Schedule those five times using an AlarmManager. You can find tutorial for it in google.
You would need to schedule alarms one by one. Foe ex..when alarm manager wakes your app the first time , then schedule a second alarm and son on.. You may not be able to set 5 concurrent alarm schedules. Do it one by one..

Categories

Resources