How can I stop the service of another app in Android? - java

On my device runs a service in an app (its not my app and I cannot uninstall it and my device is not rooted) that is always draining power. I can stop that service in my devices application settings but is there a way to stop it programatically, so I don't have to do that manually all the time ?
Could maybe something like stopService(...); work ?
Is there maybe a way to change the scheduler to prevent the restarts of the service ?
"W/ActivityManager(178): Scheduling restart of crashed service xxxx in 5000ms

Get the process id of that particular application and then
android.os.Process.killProcess(processIdKillService)

Here is a long shot (I've never tried it):
Create a Context instance for that app using Context.createPackageContext()
Use it to stop the service via Context.stopService()

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...

How to handle app kill scenario in WorkManager?(workaround)

I'm using Work-manager for some background task, as per the documentation Work will be stopped when app is killed/force-stopped until the user reopens the app next time.
Is there any way I can restart the work even after the app is killed?
I'm using onetime work request
val uploadOneTimeWorkRequest = OneTimeWorkRequest.Builder(UploadWorker::class.java)
.setConstraints(constraints)
.addTag(TAG_UPLOAD_STATUS)
.build()
Unfortunately, no. Force stop stops the application as a whole, all of your services, processes and everything permanently. When you force stop an app, it cannot even receive notifications, so, I'm pretty sure user needs to restart the app to get the job done.
By the way, getting an exception or swiping the app from recents is different, they allow work manager to process its jobs. My answer is only for explicit force stop.

Is a START_STICKY service restarted after application killed itself

So if I understand right, the difference between START_STICKY and START_NOT_STICKY is that the first will be restarted by the system, in case it kills it.
Does anyone know whether this also happens when I kill my application using Process.killProcess(Process.myPid())? Or does the system only restart the service if the service was actually being killed externally, not from within the application process?
Not always, you can't depend on START_STICKY for continuing running the service. Better way is to use startForeground for continuing running or if you want to repeat specific task then schedule your service on specific time. Also, manage it when the phone is Restarted as well.
Notification.Builder builder = new Notification.Builder(getBaseContext())
.setContentTitle("");
.setContentText("Your content text");
startForeground(1, builder.build());
Yes, the START_STICKY service is restarted after the application is killed by the Android System.
Until and unless you call stopService() method from an app component or stopSelf() from within the service, the service will be restarted if the app is killed.
But you can always make the service run using startForeground() method, in which you will have to show a notification in the status bar for your service. If you create a service using this method, your service will run at the same priority as an active activity. This means that it is highly unlikely that your service will be stopped, and no restart would be required.

Running Android App in the Background after "killed"

i'm developing an Android App for mobile sensor monitoring (accelerometer, gyroscope etc). The application is build on a class that extends from BroadCastReceiver and has communication with a service (a class that extends from Service) which is responsible of recording the sensor data when some android events occurs. When the app is running in the background works nice until I kill the activity (in this activity the user can adjust some params). I want to continue recording after the app is killed, how to fix it? Thanks!
i think you forgot to add service into your application you need to start service from your activity and then register broadcastreciever with IntentFilters after that your application will never be stop might be you will interested in this answer
You can use both of this framework to schedule your service to start at the time that you want. Like on every hour or so
http://developer.android.com/reference/android/app/job/JobScheduler.html
http://developer.android.com/reference/android/app/AlarmManager.html
but remember that if someone will go to task manager and kill your process there is no possibility to avoid that.
cheers
Solved with return START_STICKY (onstartcommand() of the service) and PowerManager inside the service to prevent the Android CPU from going to sleep. Thanks to all!!

Is it possible that the service still runs after the app got killed with task manager?

Hello im triying to run a service in background that it doesn't stop when app is destroyed by task manager. The idea of the service is verify every "x" min if there a new insert in a database that i got in a server.
The service is running great even if i close the app but when i use the task manager to destroy my app all the threads are closed too.
So i want to know if its possible to run a thread that ask in background forever unless user cancel it in the app itself, that ignore the destroy caused by task manager so in the future i can use notification bar to tell the user that a new insert happened in the database.
Tryed:
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
askServer(); // i made a timertask that ask every "x" minute
return START_STICKY;
}
As i read START_STICKY should run again the service if it get killed for some reason and i know that this can be done since some app get closed by taskmanager and still get notifications from it as whatsapp,bbms and others. Please tell me if im wrong in anything and thank you for reading!.
UPDATE: Im not trying to break any law or security rule from Android and im not trying to ignore the stoping services option from an app in settings. I want that the service that listen for new incoming "events " inserts in my case keep running after user used the interface that appear when you press home for a while :
UPDATE : sorry for talking to much about this app but is the one that i can use as an example. In whatsapp when i close the app by the interface that i showed above the process and services are killed but after a couple of second they relaunch, this is exactly what i want to do to keep user informed about database events. From setting you still can stop the service without problem or even i can put the option in the app itself to stop notifiying.
Is a bad implementation call in OnDestroy() method an instance of the service so it relaunch after destroy?
UPDATE : welp looks like my service is still running on background after i close the app. I will just have to work on my service design to not waste battery life and resources. Also i was using the log.i() to check if service was running, looks like when main process closes i can't use log or toast just notifications ( still not implemented) because the service is there running just won't show in log .
UPDATE : now is working using using startForeground(0, null). In future i will send a notification to show when a event on database happen building it and calling startForeground(1, notification).
For services, look at Settings -> Applications -> Services. and see if it is running.
However, poorly designed services may run more often or perform syncing operations. So yes it is possible.
I had a problem similar to this when developing my first android game; force-stop was the only way to kill it.
START_NOT_STICKY will kill the background service when you swipe the app away from the task manager. START_STICKY is, as the name implies, "sticky", Meaning it sticks to the android system even when the app is gone.
That's from my experience, anyway.

Categories