Android service continues running even when phone is sleeping - android

From everything I read, by default, a Service should stop running after the phone is in sleep mode. This is what I want, but for some reason, the Service is continuing to run when the phone is sleeping. I am starting it with the startService in my app's only activity. How can I make my service stop when my phone is sleeping and then start back up when the phone is turned back on?

Related

Does a foreground service need a wake lockt to stay active after screen-off?

In different sources, I read that a foreground service requires a wake lock to stay active after the device goes to sleep. But when I test it by starting a foreground service and turning the screen off while the device is unplugged (both on emulator and on a real Samsung device), the foreground service keeps running.
Does a foreground service require a (partial) wake lock to stay active after the screen is off?
From my experience of developing a timer, the answer is yes, especially when the screen is off.
Without a wake lock, the foreground service will be killed or suspended in a few minutes(2 ~ 10m in my tests). Sometimes, when the screen is off, the code won't be executed but the foreground notification still exists and the code only starts being executed after the screen is turned on. This makes debug very hard. This situation is more common if the test device is from Chinese manufacturers(Foreground service + Wake Lock + Letting user whitelist your app seems the only solid option if your app targets Chinese market).
Use a wake lock if you want your service keep running after the screen is off.

Beacon connectivity not working when app is in sleep mode

I am working on ibeacon and it is working perfectly fine when app is in foreground or when device is awake but app is in background. The issue is when device goes to sleep mode. iBeacon default methods gets stopped and start working as soon as device awakes. It does not matter whether app is in foreground or background.
I tried wake lock and alarm manager. But failed. Please suggest

Doze mode sleeps Google's Geofence sample application

I have an android application that uses Geofence and I'm having a hard time overcoming Doze mode. My manifest has the WAKE_LOCK permission and seven setNotificationResponsiveness to 0 for each region. I even changed PendingIntent to BroadcastReceiver but the app literally sleeps or is dead after a while with the deleted smartphone screen. I've already tried using LocationRequest
LocationRequest.setFastestInterval(1000);
LocationRequest.setInterval(2000);
Even so, I did not succeed. Has anyone managed to keep the application running with the Doze?
I faced the issues in My Chatting application and Location Tracking applications, While in doze mode we will not get any push, or we will not get any network connection or location details and most of the background operation will be blocked because of battery optimization, We can solve by 2 ways.
Priority push message(I used in my chatting application) - If you send normal push it will not reach the app until the mobile come to normal, If you send priority push it will reach the app you can do some process within few seconds, but this also have some limitation.
Foreground service, - For your scenario this is the best solution, You should have on service and that service should run as Foreground service so that you can get the Location information all time even in Doze mode.
WAKE_LOCK permission in the manifest is not enough for use a Wake Lock , you need to acquire and release the lock in your code, but starting with Android 6 Doze mode it goes to deep sleep also with an acquired wake_lock.
The way that works for me also with Doze mode :
In the Main Activity onStop I acquire a Partial WakeLock and I start a Service with STARTFOREGROUND_ACTION intent, the Service calls startForeground and shows a Notification.
On Activity onResume the WakeLock is released, the Service stops with STOPFOREGROUND_ACTION intent and the Service itself calls stopForeground & stopSelf.
I ended up putting my application to receive push messages. Apparently the app is no longer being killed by the operating system.

Android: Does CountDownTimer still work after the phone gone to sleep

Will the CountDownTimer work when the phone gone to sleep mode? If it doesn't, is there a broadcast receiver which I can detect whether the phone gone to sleep and awake again?

How to detect when device has resumed from sleep mode

Is there a way to detect when the android system has resumed from sleep mode, but has not turned on the screen? My service does not need to acquire any wake locks, but does need to know when the device has been woken up by another process.

Categories

Resources