Android 图片上传:使用 Activity 还是 Service?
目前,我使用 Activity 将图像上传到网络,我正在使用 AsyncTask,一切都运行良好。我目前使用一个 Activity 来实现此目的,基本上该 Activity 允许用户输入一些详细信息,拍照然后提交,显示进度对话框,直到收到响应。当使用 E 或 G 连接时,整个过程可能会相当漫长(超过一分钟)。在收到响应之前,不可能完成该活动并返回到上一个活动(这就是我想要的)。
我有什么选择。目前,如果我按下主页按钮,该过程工作正常并继续上传,应用程序是否可以最小化可编程性? 。我发现这种方法有很多问题,例如活动关闭或发生错误。
我能想到的唯一方法是将实际上传代码移至服务,使用活动收集信息并允许活动为每次上传启动新服务?然后,我可以使用NotificationManager通知用户成功或失败,并在服务中处理重试。
我注意到,当将图库中的图像分享到 Facebook 时,活动会立即关闭,并且用户会通过通知收到反馈。我假设这种方法使用服务将图像上传到 Facebook?
任何帮助/建议将不胜感激。
问候
Currently I use an Activity to upload an image to the web, I'm using AsyncTask and everything is working great. I currently use an Activity to achieve this, basically the activity allows the user to enter a few details, take a picture and then submit, a progress dialog is displayed until a response is received. When on E or G connections the whole process can be quite lengthy (in excess of a minute). Its not possible to finish the activity and return to the previous activity (which is what I want) until a response is received.
What options do I have. Currently if I press the home button the process works fine and continues to upload, can the application be minimized programmability? . I see many problems with this approach, for example if the activity closes or an error occurs.
The only approach I can think of is to move the actual upload code to a service, gather the information using the activity and allow the activity to start a new service for each upload? I can then notify the user of success or failure using the NotificationManager and handle the re-try in the service.
I noticed that when sharing an image from the gallery to Facebook the activity immediately closes and the user receives feedback via a notification. I'm assuming this approach uses a service to upload the image to Facebook?
Any help / advice would be much appreciated.
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您使用以下设置:
使用服务而不是 Activity 有一些优点。例如,您不必担心配置更改(例如更改设备的方向),您不必将用户“锁定”在他们无事可做的活动中,等等。
上传不需要UI 因此使用 Activity 并不是最好的方法。
简而言之:使用服务
I suggest you to use the following setup:
Using a service instead of an Activity has some advantages. For example, you don't have to worry with configuration changes (like changing the orientation of the device), you don't "lock" the user in an activity where they have nothing to do, etc.
Uploading doesn't require a UI so using an activity isn't the best way to do it.
In short: use a service