什么是文件上传到服务器的最佳途径? [英] What is the best way to upload a file to server?

查看:209
本文介绍了什么是文件上传到服务器的最佳途径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用android.It 的AsyncTask 办法正常工作文件上传到我的服务器。
但是,当一个文件大,并且上传到服务器需要很长的时间,如果我退出,而这个过程是持续的应用程序,在这个时候,我不想阻止我的过程中,我要上传文件到服务器,即使我关闭应用程序。
我应该使用哪种方法?

I upload files to my server using asynctask method in android.It works fine. But when a file is big and uploading it to server takes long time ,if I exit the app while the process is continuing, at this time I do not want to block my process , I want to upload file to server even I closed the app. Which method I should use?

推荐答案

使用服务做的上传。当用户决定上传文件,而不是启动一个线程或从活动本身的AsyncTask的,启动服务,并从那里做上传。这样,即使该活动被破坏该服务将继续运行,并要完成文件上传

Use a Service to do the upload. When the user decides to upload a file instead of starting a thread or an AsyncTask from the activity itself, start a service and do the upload from there. This way even if the activity gets destroyed the service will still run and should finish the file upload.

然后,你可以做一些检查,如果当上传在活动仍活着就可以显示任何你需要表现出在那里。

Then you can do some check if the when the upload is over the activity is still "alive" you can show whatever you need to show there.

看看这个: http://developer.android.com/guide/components/ services.html

注:


  1. 服务不是在单独的线程运行,因此长期运行像网络上传操作还是应该使用线程或AsyncTask的完成。

  1. Services are not run on a separate thread, so long running operations like network uploads should still be done using thread or AsyncTask.

之类的活动服务可能被系统杀死。这可能是您的整个进程将被杀死的情况。先从START_STICKY标志来通知,如果之前完成了它的工作服务就会被杀死,系统服务,系统应尽快,因为它可以重新启动该服务。

Services like activities may be killed by the system. It might be the case that your entire process will be killed. Start the service with the START_STICKY flag to notify the system that if the service gets killed before it finished it's job, the system should restart the service as soon as it can.

这篇关于什么是文件上传到服务器的最佳途径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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