Android 图片上传:使用 Activity 还是 Service?

发布于 2024-11-03 21:16:41 字数 503 浏览 1 评论 0原文

目前,我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

厌倦 2024-11-10 21:16:42

我建议您使用以下设置:

  1. 使用您的活动准备(配置/设置)用户想要执行的操作(例如,选择他想要上传的图片和目标文件夹)
  2. 当用户按“发送”时,调用将执行上传任务的服务。稍后我会注意到这样做的一些优点)
  3. 当服务完成时,创建一个通知来显示结果。您应该使用 OnGoingNotification,以便用户知道文件正在上传(它也可以有进度指示器)

使用服务而不是 Activity 有一些优点。例如,您不必担心配置更改(例如更改设备的方向),您不必将用户“锁定”在他们无事可做的活动中,等等。

上传不需要UI 因此使用 Activity 并不是最好的方法。

简而言之:使用服务

I suggest you to use the following setup:

  1. Use your activity to prepare (configure/setup) what the user wants to do (for example, choose the picture he wants to upload and the destination folder)
  2. When the user presses "Send", invoke a service that will do the uploading task. I'll note some advantages of this later)
  3. When the service finishes, create a notification to display the results. You should use an OnGoingNotification so the user knows that the file is being uploaded (it can have a progress indicator too)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文