Running Android App in the Background after "killed" - java

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

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

Is it possible to make a timer run in the background app even if the app is not in main focus?

i want my timer to keep running in the back of the app itself and when the user will re-open the app, it will go directly to that activity with the timer still running
You probably want to implement a Local Service. The usage is documented here: https://developer.android.com/reference/android/app/Service.html
By using service class and bind the service so that you can in background until you end service

Implementing BroadcastReceiver inside JobService

I am working on an application that uses JobService. Now, in my app i want to reschedule the Broadcast receiver that is working in the background. So, i want to know if i can implement BroadcastReceiver inside JobService so that it can restart after several times.
Yes it is possible. If it's background service, use PowerManager and WakeLock to keep CPU running while you you restart your service. Btw why you need to restart it?
EDIT:
if you want to keep alive your service return START_STICKY in your onStartCommand()

How to force an application to stay open?

I have seen apps like Lookout, JuiceDefender, and MagicJack run in the background indefinitely, unless force closed by a user directly through the task manager. (And even then, in Gingerbread, it wouldn't close unless you browsed to the application that was running under "Downloaded Apps" in the settings and force closed it once you were at the menu where you have options to manage the app like "Clear Memory" and "Force Close".
I am wondering how this is accomplished? I need to do something similar for an app of mine but I don't know how to avoid the Android OS's automatic task killing.. And don't say it's not possible because if that were true, JuiceDefender, MagicJack, and Lookout would not work.
What you can have is a service that stays alive indefinitely. You achieve that returning Service.START_STICKY on your Service's onStartCommand method.
Whenever the os needs resources and chooses to kill your app, your service will be respawned as soon as the resources are available again.
Bear in mind that having an application that is continuously alive will result in consuming the phone's battery. You should (at least) notify the user with a notification that your app is still alive in the background.
On top of that, you can register a broadcast receiver for the BOOT_COMPLETED event in order to restart your service while the device gets restarted. Yet, bear in mind that this could result in eating the phone's battery and so be careful on what you are doing in the service.
I believe these apps are launching a Service when their Activity get started (i.e when onCreate() is called).
A Service keeps running when the application get paused. When the Service is launched, you may return START_STICKY in your onStartCommand.
Also, to prevent a Service from being killed by Android's memory killer, you can specify that your Service is important to the user by calling startForeground(). Android Developers website states that :
A foreground service is a service that's considered to be something
the user is actively aware of and thus not a candidate for the system
to kill when low on memory.
I am creating an app and I have to use one or more of the following super functions inside OnCreate():
onDestroy()
onPause()
onResume()
onSaveInstanceState()
to close an app completely from the memory. And also do not use Activity.finish() method. Usually Android does a pretty good job in closing the app when memory is needed, called pop out of stack and not recommended to forcefully stay in memory, unless there is a very very good reason to. Hope it helps.
You can also check the Android DOC website for more information and examples to your request.
You need to start a service. Services runs in background and is useful to push alerts.
This some links about it:
http://developer.android.com/guide/components/services.html
http://www.vogella.com/articles/AndroidServices/article.html
In the service onStartCommand method return "START_STICKY".
http://developer.android.com/guide/components/services.html
/Thomas

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

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()

Categories