【问题标题】:Aws TransferManager stops when i force close my application当我强制关闭我的应用程序时,Aws TransferManager 停止
【发布时间】:2015-07-01 13:54:17
【问题描述】:

我正在从 aws s3 存储桶下载对象(视频)。一旦我打电话:

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。

【问题讨论】:

  • 准确解释这里的“强制关闭”是什么意思。
  • 强制关闭意味着应用程序停止运行任务

标签: android amazon-web-services download amazon-s3


【解决方案1】:

TransferManager 托管在您的应用程序中。一旦应用程序被杀死,它拥有的所有东西也将被杀死,包括 TransferManager。当 TransferManager 被杀死时,它会调用 finalized() 中的 shutdown() 来终止在其线程池中运行的所有传输。如果您真的希望它继续运行,那么您最好尝试服务,它可以在应用程序终止后继续存在。详情请见http://developer.android.com/guide/components/services.html

【讨论】:

  • 我尝试使用服务进行下载。我将 start_sticky 用于服务。但是,如果我的应用程序长时间处于后台,它就会关闭。你能建议吗?
猜你喜欢
  • 1970-01-01
  • 2016-10-12
  • 2019-05-18
  • 1970-01-01
  • 1970-01-01
  • 2012-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多