Locking screen while app is in foreground prevents background execution (Android) - java

I have app that is droping BLE connection in background after 10 minutes.
Everything works fine when app is connected to Debbuger if the device is locked while the app is in foreground the connection will be dropped after 10 minutes, but if we disconnect it and lock the device while the app is in Foreground , the connection is not dropped after timeout.
Are there some restrictions to Background execution when device is locked? Why does it work when connected to Debbuger?

Use Service in Foreground to create strong connection, it's the best option.
But even Foreground service doesn't help if Battery saving mode enabled, or another battery optimization modes.
Yes, debugger prevent app from being killed by system, actually not debugger but ADB connection. Sometimes test your application without USB plugged.
Also use PowerManager.WakeLock in service, it helps.

Related

how to perform long running operation using foreground services in android

When I used foreground service then when I kill the application then foreground service is automatically killed but I want to alive foreground service when the application is killed. this issue appeared in android 10 and android 11. how to solve this issue.
You can't do that as you should not be able to keep alive a foreground service after the application is killed.
You should only use a foreground service when your app needs to perform a task that is noticeable by the user even when they're not directly interacting with the app. If the action is of low enough importance that you want to use a minimum-priority notification, create a background task instead.
If you what to have some background service working, that will be possible.
You can find some useful information here :
https://developer.android.com/guide/components/foreground-services
https://developer.android.com/training/run-background-service/create-service
It should not get killed. When starting foreground service you need to create notification too. You have done that, right?
If you done everything right there is possibility that you have xiaomi phone. Xiaomi deletes everything when app killed. You need to add specific intent protection...

DOZE-Standby interfering with Bluetooth reconnection logic

I have found an issue while programming my application on an Android Device. I am trying to continuously gather data from a BLE sensor (Nordic Thingy). Everything runs smooth, but if I get to a point where the device disconnnects for a while (e.g. bad signal) the device can enter DOZE mode.
DOZE mode won't affect while there is an ongoing subscription to a BLE Characteristic and updates are being received. Is there any way to overcome the DOZE-Standby mode programmatically so that my device keeps searching for the device even if it is disconnected?
This is a key task for my project and I have't find any way for dealing with this behaviour yet.
I manage to solve this issue by implementing a Foreground Service which is in charge of supervising the connection and keeps the CPU alive preventing from entering DOZE mode.

Android - App in Power Saving Mode fails to connect to the internet while not in foreground

I have an app which I wake up using GCM notification and make it connect to the server via an https connection.
I noticed on recent android OS that when my app enters power saving mode ("apps that have not been used for more than 3 days have been set to save power") and I send a GCM notification, it wakes up and fails to connect to server (Connection Refused).
It only gains internet access if it is running in the foreground.
Any idea how to disallow my app from entering power saving mode? I need to be able to reach the device at any point. Specially that my app is DEVICE ADMIN app.

How to keep an activity work on background if I open another activity

I am using Google Voice in my android app, and I am connected the app to Bluetooth module, each time I call the Google Voice, the app try to connect to Bluetooth again, each time Google voice window Popped up, the connection lost, and once the window disappear, and the connection comes back again. So, Is there any way to keep them connected even I call the Google Voice window? Any ideas or help I will be appreciated.
You need to create background service for your problem. If you need persistent connection with your Bluetooth you can use intent service. If you are using android studio then it is very easy to create service. Just make sure you must have all necessary information about services and how to make then persistent in background.
Because of this your application will drain your device battery drastically. Make sure service is closed when application exit or all activities are finished.
See this link for more information
https://developer.android.com/training/run-background-service/create-service.html
Place the Bluetooth related processing in a Service. Make the service foreground service.
Activity can connect/disconnect from service anytime.

How to start an app on reboot if USB_HOST_ATTACHED and ACTION_BOOT_COMPLETED

I'm writing an app that needs to start up automatically when a device is connected to a tablet in USB Host Mode. In addition to that I want my app to start on tablet reboot after boot_completed if the usb device is attached. Having need to implement these two functionalities I'm in a bit of a dead lock situation. It works all fine when I plug a usb device the app brings up itself without a problem. but on reboot i'm running in to an issue where the application starts itself before the boot_complete is recieved, and that is because the usb device is already attached to the tablet. This is made worse in kitkat 4.4.2 as they have delayed processing the boot_complete state by doing it serially where as 4.3 did it parallely on reboot.
So how can I get the app to start on reboot after bootup is completed rather than before boot_up is completed while having the USB device attached.
Many thanks

Categories