How to Start forced stop application after phone reboot - android

I was trying to get to know about the possibilities of getting success of Starting up a forced stop application after the phone reboots. So here is the scenario:
My app will not have any activity. It will be a service only app where no activity will be declared. So my goal is when ever the phone restarts, it will start up the service automatically even though the service is force stopped before the phone reboots. I am using Google Pixel 6 with Android 12 rooted phone. So I have all the necessary permission to achieve the task. I have tried couple of examples from stackoverflow. none of them works. They works only if you keep running the app before the phone reboots. It wont work if the service is Forced stop before the phone restarts. Any help will be appreciated.

Related

How can I keep a BLE app alive all the time?

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.

How to make sure a service that runs when the Application is closed does not get killed?

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

Android Pie - Overnight Service Killed Consistently

I have an app which runs a background service overnight. It's triggered to run by an alarm clock app.
The app spends the night uploading data off the phone's external SD card onto Dropbox. It worked seamlessly on previous versions of Android but is not working on Pie. The background service is killed after running for about two hours every night. Interestingly, however, I've noticed that if I make a tiny change to my code, e.g. editing a string, and then run a debug, the app runs perfectly the next night but then on subsequent nights, goes back to being killed after two hours.
I've tried the following:
Using a foreground service with a persistent notification
Opening and closing an Activity after the app is opened so it's in the recent apps list
Making the app a device administrator
Disabling battery optimisations for the app
CPU and Wifi wakelocks
Running a thread with an infinite loop that uses root privileges to adjust the app's minfree values every five seconds
Disabling Pie's adaptive battery manager feature during the night
Despite all of these mechanisms, the app still gets killed. My theory is that there's some kind of artificial intelligent battery manager/performance optimiser on the phone that picks up that the app runs all night and decides to kill it in the future but then gets reset when I re-install the app.
I've tried everything and I still can't seem to find a solution. Can someone please point me in the right direction? I'm sure there's some root/reflection thing that I can do to fix this but I just don't know what it is!
I found the problem! Turns out my phone had a service called G3 which was killing the app to "save power". Apparently, this service is useless so I uninstalled it and the problem was solved instantly!
I used the following command:
adb shell pm uninstall --user 0 com.evenwell.powersaving.g3
adb shell pm uninstall --user 0 com.evenwell.powersaving.g3.overlay.base.s600ww
Pretty annoyed that this service took to killing an app that had root, administrator privileges and permission to avoid battery optimisations - how obvious do I have to make it that I want the app to stay alive?!
try job schedular
https://developer.android.com/reference/android/app/job/JobScheduler.html
https://developer.android.com/topic/performance/scheduling.html
use Alarm manager
https://developer.android.com/reference/android/app/AlarmManager.html
As one of the changes that Android 8.0 (API level 26) introduces to improve battery life, when your app enters the cached state, with no active components, the system releases any wakelocks that the app holds.
In addition, to improve device performance, the system limits certain behaviors by apps that are not running in the foreground. Specifically:
Apps that are running in the background now have limits on how freely they can access background services.
Apps cannot use their manifests to register for most implicit broadcasts (that is, broadcasts that are not targeted specifically at the app).
By default, these restrictions only apply to apps that target O. However, users can enable these restrictions for any app from the Settings screen, even if the app has not targetted O.
Nothing will work like job schedular or Alarm manager
Your problem will be only resolved by using WorkManager

Turn on/off flashlight while app is minimized

In my application I have an activity and a service. The service continously manages some data. In certain cases the service sends a broadcast request to the activity to turn on flashlight.
This works very well. However it does not work when app is minimized (manually or by incomming call). As soon as I bring the app back to the front it starts to work again.
As I observed the log I saw that the service is still running when app is minimized. However since the activity is not present anymore the camera object cannot be accessed to activate the flashlight. But there is no error in the log.
Why is the flashlight not activated? How can the flashlight be activated when the app is minimized?
Only the foreground activity can hold the camera. From the Google documentation:
"If your application does not properly release the camera, all subsequent attempts to access the camera, including those by your own application, will fail and may cause your or other applications to be shut down."
You can try claiming it on the service instead, but I suspect that won't work well. It just really wasn't designed to work this way.

background service makes app crash after unlocking the real device

I am running a background service which checks for some updates from the server every 2 hours in my android app and it works fine so long as the device is the not in the locked state. But once I lock my phone and unlock again I see my device displaying the message that my app has crashed. What could be the real reason behind it. Does it need some permissions to be declared. Could anyone suggest me as to why this is happening.
It can be happening from several causes. But I gonna give my guess: are you taking in consideration the case that your app fails to reach the server? What I mean is that I'm guessing that when you lock your phone, or close it in some way, it can be closing your connection and your app can be crashing because it fetchs the server without connection. Its only a very broad guess...

Categories

Resources