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

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

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.

Android: service looses sensor focus when activity is closed

I'm making an app where a background service does some stuff on some specific motion. At the moment I use a simple Service with a SensorEventListener attached to register the movement of the phone and to do the action I wanted it to do. Im also using an (optional) wakelock without any time limitations (but it gets unatached when the service is terminated) to ensure that the service is always accessable. I used the START_REDELIVER_INTENT to ensure a restart when the activity stops it when the user stops its (regularly via the ome button).
Now to my problem: When I stop the activity, the service gets terminated but only sometimes restarted (I dont know whether its not restarted or just doesnt get its sensorchanged called. I suspect last but I dont know for sure due to I only place a simple Sysout in the Sensorchanged method for testing).
Is there better and more efficient way to achive my goal (eg startForeground) and how can I ensure a continous aquiring of sensordata after an activity stop?
Services which need to run continuously have to be started with startForeground().
A positive sideeffect of this is that the user will be aware of your app using a sensor and can stop it if he runs low on battery.

Making sure android app is running

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.

Having my app run for several hours causes android phone to freeze

Whenever I leave my app running for awhile (~9 hours last time) whatever activity the phone has up will stop responding and need to be force closed, after which there's just a black screen below the slide down notification area and pressing back or home changes nothing. I cannot turn the phone off normally either, as it just spins endlessly when trying to turn the phone off. I need to remove and reinsert the battery.
My app has a background service which monitors for a bluetooth device and runs accept threads with 30 second timeouts. I believe I'm handling them properly as there's only ever 1 shown in my debug screen. I have seen this phone freezing behavior while having the device connected for the entire duration and not having it connect at all.
I have tried keeping the phone connected to logcat to see what happens when the phone freezes up but it always stops receiving updates from the phone after some time, maybe an hour or two at best? Hard to estimate since I'm usually doing something else while waiting.
Does anyone have any tips for what could be causing this or how I can get some additonal feedback to work with?
if your service runs in the background , the OS might kill it after some time , and you must know how to handle it within the service .
services that do a lot of work will be good candidates of being killed .
if you wish your service to do long work (30 seconds is quite a long time) , you should set your service as a foreground service , with a notification .
if you don't wish your service to be a foreground service , try to recover your state when being killed , and try to optimize your code further .
for more information of how services run on android , read this link .

How to make a better battery-saving data update service for an android app

I am building an android app for fetching internet data and rendering it as a list. The data is changing every minute, so I made a service and used a Timer to load the data with an interval.
My question is that:
I want to know when the app (not a particular activity) goes to the background, for example, user pressed the home button, in that case, I want to pause the service in order to save battery.
I want to know when the phone is sleeping (screen dimmed), in that case, I would like to pause the service too.
Any ideas?
Pause your service in onPause (and resume in onResume).
Unless you have a wake lock, your app will not run when the phone is sleeping.
If you're bound to a foreground app, why use a service in the first place?
To elaborate on 1: I understand you're saying "app", not "a particular activity", but your best bet is to use Activity.onPause and Activity.onResume. If anything, you can add a short delay so that nothing happens if onPause is followed immediately by onResume.

Categories

Resources