Android run long process from status bar
I have an application which once the user runs the app it has a long process to run, such as downloading needed files and setting it all up and what not. I would like to move the process to the background and show a notification 开发者_如何学运维in the status bar while the process is running.
What would my best strategy be to keep the process alive while downloading the files and what not. I've read about services, but have also heard they're easily killed? Should I use a service or should I just run a thread with maximum priority and just throw a notification up and close it when the process is over?
What's best thank you for any help. Process by the way is about ten minutes no longer than it takes to download a rom in rom manager basically just want that same setup thank you for any help.
It sounds like a Foreground Service is exactly what you need. When you create it, a notification is placed in the status bar to tell the user that it is running. The service is also given high priority so that it will not be canceled.
From the documentation for the Foreground flag:
public final void startForeground (int id, Notification notification)
Make this service run in the foreground, supplying the ongoing notification
to be shown to the user while in this state. By default services are background,
meaning that if the system needs to kill them to reclaim more memory (such as to
display a large page in a web browser), they can be killed without too much harm.
You can set this flag if killing your service would be disruptive to the user,
such as if your service is performing background music playback, so the user
would notice if their music stopped playing.
You can look at the rest of the documentation here.
A service is exactly what you want. You can specify how high of priority your service is when you create it (see @theisenp's post).
As for moving your Activity
to the background and reopening it later - I recommend starting the service, then actually stopping your app using the finish()
method. Then, once the service is complete, relaunch your activity with Intent, or broadcast an Intent that can be handled by a BroadcastReceiver
.
精彩评论