Making sure android app is running - android

I have developed an android app that needs to run in the background and alert the user after a certain amount of time. The problem is I have is that if I set the time and do something else on the phone, the notification doesn't come through. (I tested this by setting the timer for 10 minutes). Is there a way to make sure the app is always open in the background?

Did you use a Service? If not, it is definitely not running in the background. As soon as an Activity is withdrawn from focus, its onPuase method is invoked and it stops running (see Activity lifecycle). To make your app working in the background you need to implement a Service.

Related

How can I keep a countdown timer running even if I don't open the app for a long time?

I've been making a timer app using CountDownTimer, and now I'm testing if it works properly. However, if I set a bit longer period, for example 1h30m, and close the app, it stops working, and when I open the app, it shows the initial screen, the timer I set disappears.
I tried to turn off "Battery Optimization" in this app.
I tried to turn on "Background Work" in this app.
But they didn't work.
You don't. You persist the time the countdown counts toward when the user starts it. If the application closes,then the user opens the application again, you set up the CountDownTimer component with the proper remaining time. If you want to do something when the countdown ends, even when the user is not using the application, you can use AlarmManager to wake up the device.

My Service is idling when the device goes into power saving mode

I'm currently struggling to keep my service alive when trying to get information about the user for a study. We are using a foreground service, which runs a timer on a 5-sec interval checking a resource of the user. As that resource is only relevant if the screen is on, we could stop the timer whenever the screen goes off. For this, we are using a broadcast service. All in all, this works well. The problem occurs, if the user closes the app and then puts the device on standby -> screen goes off. We would like to stop the timer and restart the timer inside that service if the screen goes on again. Now theoretically that works. I tested it with a counter variable which increments each time the run is called inside the timer. After that, I update my notification and show that variable. If however I close the app and the screen goes off, this variable is no longer updated in the notification and ergo, my resource check is not running. But I found out, that once I restart the app, the variable did indeed increase every 5sec and is then updated in the notification again. Furthermore, it is working if the device is on power/getting charged. So I guess it is a power management thing.
Summary: The ForegroundService/BroadcastReceiver is not acting like I would want to if the app is closed and the screen goes off, even though a part of it is working as intended. Works completely if the device is charging.
Do you have any ideas on how to avoid this behavior and allow the service to rerun the timer correctly so that the methods inside will work again?
Best regards,
Yukko

Android: Timer keep timer active when app in background - or AlarmManager?

i use a timer in my app to start an async task (reading email) and put some status to the GUI - that works fine, as long as the app is not in background - then the timer seem to stop. Maybe it's normal.
Then i used the search function and read about the AlarmManager - but i want the timer only to run when my app is started.
How can i make sure the timer fires/works, no matter if the app/activity is in foreground / what would you recommend?
If I undestand well, you want a timer which runs only when the app is started, no matter whether it is in foreground or not. I see two options :
Set a timer in your activity : it can still live when activity is in background (onPaused or onStopped) but it can not interact with the GUI. So, you could store data in some way, and use it when activity is resumed.
Other option is to use Android services, that can continue to live with your application and execute tasks even in the background. As previously, you can retrieve data from them when the activity is back.
You should read the official documentation
Indeed, alarm manager is designed for planning tasks even if your application is stopped.

Android app do something on close service/app

I have an app which has a queue in the background to do something. I also have a service which shows and updates a notification in the notification-center about the progress of the queue (because it is not depending on a certain activity).
Notification be like: "there are 4 items left".
All works great except the app is destroyed (by the user and/or system).
I want to update the notification bevore the app or the service is closed (to something like "come back, you missed some items"). I know there is no "close"-event for an application and the "destroyed" event from the service is not called in this case.
I'm working with Xamarin.
What can I do to achive this? Any ideas? Thanks
You can initiate your background service using Alarm Manager even if it is killed. I have faced the same problem when I had to update the user's current location. I set an infinite alarm which starts android service if its not started in every five minutes. You can give it a try.

Bring dialer app into foreground from background when phone call is finished

This is the current mechanism:
I make a phone call from my app (app stays in the background)
Phone call ends
The system fires android.intent.action.view and my app listens for it, so I can start my app to show call log.
The problem is, that a new instance of my app is being created, instead of taking the other into foreground. This is problem because loading the app again needs some time, and I need an instant, very fast action.
I would like to avoid the loading time, so this would be the ideal solution:
...
3. System fires intent, and my background app comes into foreground (instead of launching it again) and does it's job
I have read that posiibly it cannot be done because of security, but my app would start anyway, I just want to make it faster, so I hope there is a solution.
Have you tried setting android:launchMode = "singleInstance" in your androidmanifest.xml?
I solved it with new task flag and passing parameters in phonestatelistener. Interesting that sometimes phone state changes during calls almost "at random", so it gives a hard time to handle the cases on all phones and os versions.

Categories

Resources