I'm about to develop an application for Android.
I need to continuously run an update thread that executes a task in a given interval. And this thread needs to be shared between 开发者_StackOverflowactivities.
I would also like to notify the active Activity
when the thread executes a task.
In iOS I would execute a thread from the AppDelegate
and then send notifications, but how do I achieve this with the Android SDK?
I'd say create a Service that does the work for you. When it starts or finishes (or at any point you want), you can send a custom broadcast intent to indicate all parties that your service has passed this point. See Intents.
If you want to start this Service
periodically, also have a look at the AlarmManager. That allows you send a broadcast intent periodically - in this case the intent that starts the service.
Also note that it's usually wiser to to terminate a service via stopSelf()
when it's work is done. Run it again via intent when needed. Keeping it alive all the time isn't a good idea when it comes to battery life.
Make a service
and implement a thread with infinite loop
and from there you can do your job....to update data in activity you can make a static method and you can call it from there with appropriate arguments....
精彩评论