Beacon connectivity not working when app is in sleep mode - android

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

Related

Set an alarm in android

I'm trying to set up an alarm once a day so my user knows that he needs to check the app (extracts data from bluetooth devices, etc)
So far, I have used AlarmManager.setExactAndAllowWhileIdle(...), and a broadcastreceiver that plays some sound in the onReceive method, but this won't work when app enters App Standby.
Anyone knows how to properly set an alarm that prompts no matter if the device is in Doze or the app is in App Standby?

No services are working on One plus 5T OS android OS 9.0

I have created the application for getting the Users latlong and send them to the server. So, now we have created the Foreground services for the getting the USER's location and is working fine in the All the other devices including the Android OS 9.0.
But in the specific OnePlus 5T Android 9.0 device the foreground service stops working when the device is locked and Service is also terminated after one hour, also even after we have disabled the deep cleaning option there.
Please let us know if there is something with the Device specific config that we need to change for working foreground service continuously.
Reason behind that- Alarm manager is not calling on time even if you are using setAndAllowWhileIdle() or setExactAndAllowWhileIdle() it won't work and pending intent also get destroy, Only High priorities task get executed in system like FCM push notification.
So the solution I found is :- implement push notification in your android application and send data message through push notification and start service from
public void onMessageReceived(RemoteMessage remoteMessage) {
//start your service from this method
}
note :- Check your Force Stop button of that application is enable or disable
if its disabled that means system complete killed your application
Android doze-standby mode and idle mode

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.

Push message cannot start service at once

Context
We are developing an android app that is supposed to do the following:
the user installs the app, registers and closes the app
once or twice a year an admin sends a Firebase data message with priority high to the user containing a geo fence
the FCM message starts a JobService that locates where the phone is
if the phone is inside the given area an Activity is started and user interaction starts
if the phone is outside the area the service is stopped and the user is never disturbed
I developed the app based on the Firebase push example found here
The problem
The application works fine on my older phones but on my new test phone (android 8.1.0, LineageOS 15.1) we have a problem when the phone is in sleep mode. In this case the FCM message arrives instantly but the service is first started once the phone is unlocked. So the message is stuck between 2. and 3.
We need the app to respond at once - not when the user decides to use his phone 2 hours later.
I assume the problem is due to the Doze mode introduced with android 6. I tried to solve it by adding the app to the whitelist in settings->battery->battery optimization but this did not solve the problem.
Questions
Is it Doze mode that delays my app between 2. and 3.? If so why is it not solved when the app is in the whitelist?
Is there any other way to start the location service at once? This post suggests that a foreground service can do it but this requires that a notification is shown which breaks with 5.
Is there another way to start my service at once besides whitelist and foreground service?
Yes! you are right this may be due to the Doze and App Standby features introduced in API 23 Marshmallow.
As mentioned in the documentation, the system do ignore wakelocks and system doesn't allow JobScheduler to run, which effectively prevents your app from running the Job.
An easy and effective workaround would be to run Location detecting routine inside a normal background service and start it using startService() when you receive FCM push.
Note that you might still need to white-list your app because as mentioned in another post here, only a whitelisted app can use the network and hold partial wake locks.
Is it Doze mode that delays my app between 2. and 3.?
From the documentation Doze mode affect Network access and blocks JobScheduler.
If so why is it not solved when the app is in the whitelist?
Also from the documentation: An app that is whitelisted can use the network and hold partial wake locks during Doze and App Standby. However, other restrictions still apply to the whitelisted app, just as they do to other apps.
So the blocking of JobScheduler still applies.
Is there any other way to start the location service at once? This
post suggests that a foreground service can do it but this requires
that a notification is shown which breaks with 5.
Ignore Battery Optimizations (for Internet access) in combination with an AlarmManager with setAndAllowWhileIdle() or setExactAndAllowWhileIdle() should work.
Be careful with Ignore Battery Optimizations
Google Play policies prohibit apps from requesting direct exemption from Power Management features in Android 6.0+ (Doze and App Standby) unless the core function of the app is adversely affected.
I think an important question here is: Do you really need to execute the JobScheduler immediately.
If a user leaves a device unplugged and stationary for a period of time, with the screen off, the device enters Doze mode.
If the device is in Doze mode, it means the user is not using it.
if the phone is inside the given area an Activity is started and user interaction starts
This is the step Doze blocks
We need the app to respond at once - not when the user decides to use his phone 2 hours later.
If the device is in Doze it means the user is not interacting with it. Even if you show the Activity the user is not using the phone, he will see it when he starts using it 2 hours later :)
I still didn't try that,
but you might use a WakefulBroadcastReceiver:
https://developer.android.com/training/scheduling/wakelock.html
https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html
According with this links, you should declare your receiver as:
public class YourReceiver extends WakefulBroadcastReceiver {
Probably your receiver is already a WakefulBroadcastReceiver because notifications are showing..
In the receiver, you start the service (your service has to be an IntentService) using:
startWakefulService(context, your service);
Finally, remember to release the wake lock in the service:
#Override
protected void onHandleIntent(Intent intent) {
<Your_broadcast_receiver_class>.completeWakefulIntent(intent);
}
I hope it helped

Android service continues running even when phone is sleeping

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?

Categories

Resources