I'm trying to connect an App to a custom system with BLE that I added to my dog's door, which allows me to open/close it remotely as well as to know when is open "illegally" so it reproduce an alarm in my phone.
I've added the BLE class to the Main Activity and manage it from there, using fragments to see the data and sending commands.
But the problem I'm having is that, after some time, the App just close itself, breaking the BLE connection so I can't receive updates in "real time".
I don't know if is possible that the system don't kill the App, I've hear about services and that they keep the connection alive, but I'm not sure if they respect the other parts of the App or if the App get killed but the service don't. I've never worked with services before.
I've added the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission and also disabled the memory optimization as well but it still get killed.
When the app is killed and I press the App icon to open it, it looks like a fresh start and it should be keep alive all the time. Otherwise I will have "security issues" if something happens while I'm not connected.
Related
I am developing a Fitness Application as part of my Bachelor Thesis, and want to keep track of step counts even when the application is completely closed. For this I am currently starting a service that utilises the built in Sensors "Step Counter" and "Step Detector". After some testing I found out that sometimes my Service gets killed and no longer keeps track of the steps taken. I left the phone on my desk overnight and walked around in the morning then I opened the application and the steps I took in the morning were not tracked, whereas when I close the application and immediatly start walking the tracking of steps still works.
Is there a way to make sure that my Service does not get killed?
Would the use of a Foreground Service solve my issue and are there any alternatives to using a foreground service?
Foreground Service is the only way if you want to assure that the service will not be killed.
The reason for this is that the foreground service always shows a notification to the user and can be killed by the user if he wants to, this is especially important if you want to know for sure what runs on your device.
All previous methods of making permanent running services are deprecated starting from android 9, when a new privacy policy was introduced.
Basically you need to keep service running in the background,
Here is the workaround to achieve this
https://stackoverflow.com/a/58162451/7579041
above link is useful for Stock ROM & Custom ROM Devices like OnePlus, OPPO, VIVO, etc
I hope this will help you out
I am developing an application for a business entity. That application should run in the background in every employees' mobile phone. Employees are mostly salesman. The application basically detects location changes and suggest the salesman where they might visit. A kind of reminder application. It also lets other salesmen see where are their teammates.
Right now I am using a foreground activity and it works fine till the system forcefully doesn't kill the service or the phone doesn't reboot due to manual activity or battery discharge.
Ones the application is closed, as of now, the managers in the firm needs to call salespeople to turn on the application once, as on application start it automatically turn on its foreground service. But this is really an extra burden on the management team which can be automated.
I am ok to have any settings based or code based solution. One solution is to root the phones of salespeople and install some extra utility app or write the code based on root APIs, but this will be too much for this simple task.
THe permission RECEIVE_BOOT_COMPLETED was not added properly in the manifest. After adding the permission it worked calmly. In on receive method of the broadcast receiver, I am starting the foreground service.
At the moment, the best way is to use WorkManager https://developer.android.com/topic/libraries/architecture/workmanager/ Yes, it still alpha, but works very good.
From other side, you could work on automating the task "managers in the firm needs to call salespeople to turn on the application once". I mean, an app/backend could automatically call the salesman (with some pre-recorded message) or send SMS to them.
I am a beginner in Android development. I want to build a service that once started, must keep running.
Step 1.) Once user will install the app give some input, service will start. (i'll use IntentService)
Step 2.) Now user moves to other app/close my app. I want to make sure Service should keep running and processing/saving info with current timestamps.
Questions:
1.) How I make sure that the service keeps running/saving data even if user returns after say, 1 month on my app.
2.) I read that every app is a new user and separate VM is provided by the Android, question is when an app is considered to be closed?
3.) If my app closes, related services will also close then?
1.) How I make sure that the service keeps running/saving data even if user returns after say, 1 month on my app.
Once user installs app, you can start STICKY service but make sure you inform the user as well as Android OS by making it run on foreground with on going notification. it will help your service to run for long time without being killed by Android OS.
1 month is long period, user may reboot the cell in between. and you will need to restart your service. so you should register for reboot complete event .
2.) I read that every app is a new user and separate VM is provided by the Android, question is when an app is considered to be closed?
There is no condition like considered to be closed.Actually, when user installs app, it is by default in stopped mode, it gets activated when user opens your app for first time. and application can go back to stopped mode again whenever user force stop your app.
3.) If my app closes, related services will also close then?
If you are considering that app is considered as closed when user don't use the app,your service won't close unless Android OS kills it in low memory situation.you may make it foreground as suggested earlier to convey Android OS that you are doing important things,nothing is wrong,so kill me only at last when you don't have any other options left
I'm developing and Android application on CodenameOne that needs to send a web request every 5 minutes even when minimized. How can I achieve this behavior in order to prevent that the request get stopped or paused by the OS?
You cant do that from the activity, you'll need to create background service.
http://developer.android.com/training/run-background-service/create-service.html
Use AlarmManager to set up your every-five-minute operation. Have it trigger a BroadcastReceiver, which in turn passes control to my WakefulIntentService (or your own IntentService that handles the WakeLock, that you will need so that the device stays awake while you do your work). Have the service do the "web request".
This is still not 100% guaranteed:
The user can Force Stop you from Settings, in which case your alarms are gone and nothing will happen until the user manually runs your app again
The user might block alarms from firing on certain devices, like various SONY Xperia models, that offer "stamina mode" or the equivalent
However, it is the best that you are going to get, short of rolling your own Android OS version.
The other guys answers are correct that you need to create a service but they somehow ignored the mention of Codename One.
Under Codename One you need to create a native directory for android and just place the service source code there (just use a blank service class that doesn't really do anything). Then you need to add to the build arguments the option android.xapplication where you would state the service XML attributes.
Having said that what you are trying to do is VERY wrong and you shouldn't do it in Android! You will drain the battery life from the device in no time and the service will be killed by the OS eventually (since it will be a battery drain). The solution is to send a push notification to the device to wake up the application.
In Android 9 and newer you can prevent your App falling asleep with a battery setting.
Long click on your App -> App info -> battery -> optimize battery consumption
Here add your App from the list.Hint: maybe the menu entries have a different name, depending on your phone.
I'm making a parental control/accountability app for android. It consists of a monitoring service that runs in the background and starts when the phone is booted.
Unfortunately, I have found that when android is started in "Safe Mode", services are not started automatically, and because of this my app has a serious flaw.
While in safe mode, the web and other apps can be started without my monitoring service running.
I thought that if it isn't possible to monitor app activity while in safe mode, maybe I could at least have my app detect if the phone was previously in safe mode. Then it could maybe alert the parent or accountability partner?
Does Android keep any log of this? Or any boot log in general?