Android Question Kill Background Process??

ilan

Expert
Licensed User
Longtime User
hi

i would like to start a Service that will kill my app after x minutes.
i am using ActivityManager for that but nothing happens.

i have added the needed permission to manifest and i am also checking that the activity is running before i try to kill it but nothing happens.

i am using android 6+.

i have also read here in some threads that others have the same issue.
is it at all possible to kill an app from a service? and also if it is the app that started that service?

i found this: http://stackoverflow.com/questions/9563404/kill-another-process-application-programmatically
 

ilan

Expert
Licensed User
Longtime User
Is the service part of your app? Call StopService(Me) and ExitApplication. It will kill the process.

i tried it but it didnot worked

i called Exitapplication and then StopService("")

it says in description to use an empty string if i want to close the service itself.

EDIT: after doing it i still see the app running in background. and also when i return to the app only activity_resume is fired because i exit the app via Homebutton so that means the app was not closed from that service, right?
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
It is better to first call StopService.

It should work assuming that the service is part of the same application. It will kill the process. Android may restart the process automatically. But FirstTime will be true in that case and it will start from Activity_Create.

oh, i understand. but this is something that i dont really want.

the thing is that if the app was closed from the homebutton than it will run in background and because i have some CLV with images in it, it usage something about 100MB ram.

the problem is if you have any CLEANxxx (RegClean,..) App you start get warning after x minute about an app that consume a lot RAM in background so my client ask me to kill the process after 10 minute so if a user has such a CLEAN app installed he wont get warned.

and if the app will restart then this is something i dont really want.

i could clean the CLV on Activity_Pause but when the user return it will take again about 3 sec to rebuild all CLV's so i would like to keep them running for few minutes because it happens that the user return after 1-2 min and i would like to have a quick start. but after 10 minutes the app should be killed like i would call ExitApplication from it.

is something like this possible?

thank you :)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
i called Exitapplication and then StopService("")
I asked for a way to know if some library starts an own service, because if it is so you cannot kill your app without stopping this service, I think.

However, I would try (supposing to have only the Starter service):

' Activity - somewhere
B4X:
CallSubDelayed(Starter, "Kill")
Activity.Finish


' Starter service
B4X:
Sub Kill
    StopService(Me)
End Sub

Sub Service_Destroy
    ExitApplication
End Sub


All other Activities and Services should be already Finished / Stopped, of course.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
I don't understand completly your post #5 but I think it is wrong to fill a CLV with 100Mb of data; you should implement a kind of pagination

i am not filling the CLV with 100mb it is just a simple list with 60 items that has 1 image view and few labels.
the result is that the app consume 100mb of ram. it is not only the CLV but i believe the CLV is not a small part of it.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
is something like this possible?
Yes. It is not recommended but it is possible. Apps should let the OS decide when to kill processes.

1. Add a timer to the starter service.
2. Enable it in Activity_Pause of the activity. If there are multiple activities then you will need to do a bit more work.
3. Disable it in Activity_Resume.
4. Stop the service in the tick event and kill the process with ExitApplication.
 
Upvote 0
Top