Android服务多线程设计 [英] Android Service Multiple Thread Design

查看:105
本文介绍了Android服务多线程设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于android和服务的一个新问题。目前,我正在开发应用程序,应该将图像发送到服务器。还可以发送更多的图像并行。



我为每个图像创建了一个新图像的服务。该活动可以绑定到该服务并收集关于进度的信息。我想在通知中显示每个图像的当前状态(当用户点击通知时,应该显示具有该图像进度的活动)。



但是我遇到了几个问题。有绑定的错误,通知挂起的事件开始活动完全新的,所以我失去了关于当前发送图像的信息等等。



有人可以告诉我,如何我可以以适当的方式设计这样的问题。



thx

解决方案

p>我将使用在您的应用程序类中创建的Controller对象。
这个控制器对于你所有的活动是一样的。如果图像上传启动,控制器将创建一个新的服务/线程并监视它的状态并显示通知。每个活动现在可以向控制器询问其过程的状态,并向用户显示相应的信息。生活在应用程序生命周期中的控制器将绑定到服务而不是短暂的活动



在droidcon上有一个关于这个你可以找到在幻灯片放映上滑动。当解释使用的架构标记时,它在幻灯片的后面部分变得有趣。我与同样的问题战斗了几个星期,并得到了同样的结果,我很高兴终于有一种资源存在于这个主题的网上。


A new question about android and services. Currently I'm developing a App that should send images to a server. It should also be possible to send more images parallel.

I made a service that creates for every image a new image. The activity can bind to that service and gather information about the progress. I want to show the current status for every image in a notification (and when the user clicks a notification, an activity with the progress for that image should be shown).

But I get several problems with that approach. There are errors with binding, the notification pending event starts the activity completly new, so I lose information about currently sending images and so on.

Can someone plase tell me, how I could design such a problem in a appropriate way.

thx

解决方案

I would use a Controller object that is created in your application class. This controller is the same for all your activities. If an image upload is started the controller creates a new service/thread and monitors it state and shows the notification. Every activity now can ask the controller for the state of its process and show the corresponded information to the user. This way the controller living in the application lifecyle will bind to the service and not the short lived activity

There was a talk from mark brady on droidcon about this you can find the slides on slideshare. It gets interesting in the later part of the slides when the architecture mark used is explained. I fought with the same problem for some weeks and came to the same results and I'm glad that finally some kind of resource exists on the net on this topic.

这篇关于Android服务多线程设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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