2015-04-23 76 views
0

我正在從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。

+0

請正確解釋**您在此處的「強制關閉」是什麼意思。 – CommonsWare

+0

強制關閉意味着結束正在運行的任務 – Vinay

回答

1

TransferManager駐留在您的應用程序中。一旦應用程序被殺害,它擁有的所有內容也將被終止,包括TransferManager。當TransferManager被終止時,它會調用finalized()中的shutdown()來終止在其線程池中運行的所有傳輸。如果你真的希望它繼續運行,那麼你最好嘗試在應用程序終止時可以生存的服務。有關更多詳細信息,請參見http://developer.android.com/guide/components/services.html

+0

我嘗試使用服務進行下載。我使用start_sticky進行服務。但是如果我的應用程序長時間處於後臺,它會被關閉。你能建議嗎? – Vinay

+0

請參閱我的編輯 – Vinay