当我强制关闭我的应用程序时,Aws TransferManager停止
问题描述:
我正在从aws s3存储桶下载对象(视频)。一旦我打电话:当我强制关闭我的应用程序时,Aws TransferManager停止
TransferManager transferManager = new TransferManager(s3client);
GetObjectRequest getRequest = new GetObjectRequest(bucket, entity.getName());
String s="";
download = transferManager.download(bucket, entity.getName(), f);
所有的对象默认情况下在后台下载,即使我离开我的应用程序或者把我的应用程序上的背景。
但如果ü强制关闭(意味着我长按我的家按钮并关闭运行列表我的应用程序),我的应用程序中的所有对象停止下载
什么是可以轻松地下载运行在后面的方式,即使应用程序停止...
我服务尝试,以及:
public class MyDownloadingService extends Service {
public static Download download;
File f;
public static ArrayList<DownloadEntity> downloadList;
public class LocalBinder extends Binder {
public MyDownloadingService getService() {
return MyDownloadingService.this;
}
}
private final LocalBinder mBinder = new LocalBinder();
@Override
public void onCreate() {
super.onCreate();
// downloadList=new ArrayList<DownloadEntity>();
//Toast.makeText(this, "Service Created", 300);
}
@Override
public void onDestroy() {
super.onDestroy();
//Toast.makeText(this,"Service Destroy",300);
}
@Override
public void onLowMemory() {
super.onLowMemory();
//Toast.makeText(this, "Service LowMemory", 300);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Toast.makeText(this,"task perform in service",300);
if (intent!=null) {
String bucket = "", object = "", file = "";
if (intent.getExtras() != null) {
if (intent.getExtras().getString("bucket") != null) {
bucket = intent.getExtras().getString("bucket");
}
if (intent.getExtras().getString("object") != null) {
object = intent.getExtras().getString("object");
}
if (intent.getExtras().getString("file") != null) {
file = intent.getExtras().getString("file");
}
new downloader(bucket, object, file).execute();
}
}
return android.app.Service.START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public static void getList(){
if (downloadList!=null) {
SoapCostants.downloadList = downloadList;
}
}
public class downloader extends AsyncTask<String, String, String> {
String bucket,object;
String file;
public downloader(String bucket,String object,String file) {
this.object=object;
this.file=file;
this.bucket=bucket;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
File f=new File(file);
// Toast.makeText(this,"Service start",300);
TransferManager transferManager = new TransferManager(S3Getter.s3client);
try {
GetObjectRequest getRequest = new GetObjectRequest(bucket, object);
String s="";
download = transferManager.download(bucket, object, f);
DownloadEntity entity2=new DownloadEntity();
entity2.setKey(object);
entity2.setValue(download);
if (downloadList==null){
downloadList=new ArrayList<DownloadEntity>();
}
SoapCostants.downloadList.add(entity2);
downloadList.add(entity2);
for (int i = 0; i < SoapCostants.downloadedList.size(); i++) {
//SoapCostants.downloadedList
if (object.equalsIgnoreCase(SoapCostants.downloadedList.get(i).getName())) {
SoapCostants.downloadedList.get(i).setIsDownloading("yes");
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
}
}
我用start_sticky的服务。但是如果我的应用程序长时间处于后台,它会被关闭。或者当我强制关闭我的应用程序时,它会关闭。 我通过调用上面的服务类的getList()来检查它。但它返回null。
答
TransferManager驻留在您的应用程序中。一旦应用程序被杀害,它拥有的所有内容也将被终止,包括TransferManager。当TransferManager被终止时,它会调用finalized()中的shutdown()来终止在其线程池中运行的所有传输。如果你真的希望它继续运行,那么你最好尝试在应用程序终止时可以生存的服务。有关更多详细信息,请参见http://developer.android.com/guide/components/services.html。
请正确解释**您在此处的“强制关闭”是什么意思。 – CommonsWare
强制关闭意味着结束正在运行的任务 – Vinay