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

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

问题描述

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

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.

谢谢

推荐答案

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

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

mark Brady 在 droidcon 上有一个关于这个的演讲,你可以在 slideshare 上找到幻灯片.当解释使用的架构标记时,幻灯片的后面部分会变得有趣.我与同样的问题斗争了几个星期,并得出了同样的结果,我很高兴终于在网上找到了关于这个主题的某种资源.

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天全站免登陆