分享不同势活动之间线程 [英] Share a thread between diffrent activities

查看:87
本文介绍了分享不同势活动之间线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要开发Android应用程序。

I'm about to develop an application for Android.

我需要不间断的运行执行给定INTERVALL任务的更新线程。而这个线程需要被activites之间共享。

I need to continously run an update thread that executes a task in a given intervall. And this thread needs to be shared between activites.

我也想通知活跃活动当线程执行的任务。

I would also like to notify the active Activity when the thread executes a task.

在iOS的我会从的AppDelegate 执行一个线程,然后发送通知,但我怎么achive这与Android SDK?

In iOS I would execute a thread from the AppDelegate and then send notifications, but how do I achive 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.

如果您要启动这个服务定期,也有看的 AlarmManager 。这使您定期发送广播的意图 - 在这种情况下启动该服务的意图

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.

另外请注意,它通常是明智的,终止通过 stopSelf服务()时,它的工作已经完成。在需要的时候通过的意图再次运行。保持它一直活着,是不是当涉及到​​电池续航时间是个好主意。

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.

这篇关于分享不同势活动之间线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆