Android的如何停止/终止/取消媒体上传或下载 [英] Android How to stop/kill/cancel Media Upload or Download

查看:381
本文介绍了Android的如何停止/终止/取消媒体上传或下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的AsyncTask上传或在我的谷歌驱动器下载文件。

I create a AsyncTask to Upload or Download files on my Google Drive.

这是上传文件我的code的一部分的例子:

This is an example of a part of my code to upload a file:

public class AsyncUpload extends AsyncTask<String, String, String>{

    Context c;



    public AsyncUpload(Context c) {
        super();
        this.c = c;
    }


    @Override
    protected String doInBackground(String... params) {



        try{

            // File's binary content
            java.io.File fileContent = new java.io.File(localFilePath);

            InputStreamContent mediaContent = new InputStreamContent(FileInfo.getMime(localFilePath), new BufferedInputStream(new FileInputStream(fileContent)));
            mediaContent.setLength(fileContent.length());


            // File's metadata.
            File body = new File();
            body.setTitle(fileContent.getName());
            body.setMimeType(FileInfo.getMime(localFilePath));

            if(parentId!=null){
                ParentReference parent = new ParentReference();
                parent.setId(parentId);
                body.setParents(Arrays.asList(parent)); 
            }


            Drive.Files.Insert request = drive.files().insert(body, mediaContent);
            MediaHttpUploader uploader = request.getMediaHttpUploader();
            uploader.setDirectUploadEnabled(false);
            uploader.setChunkSize(MediaHttpUploader.MINIMUM_CHUNK_SIZE); 
            uploader.setProgressListener(mediaHttpUploaderProgressListener);
            File file = request.execute();
            if (file != null) {
                Log.i(TAG,"File Uploaded");
                isAttempt=false;
                return file;
            }

        } catch (IOException e) {
            Log.e(TAG,"IOEXCEPTION Message:\n"+e.getMessage());
        }


        return null;
    }

}

这code完美的作品,但我不知道如何阻止它而文件上传(或类似code下载)!

This code works perfectly but I don't know how to stop it while a file is uploading (or downloading with a similar code)!

请帮帮我!

非常感谢

推荐答案

试试这个:
第一次调用cancel方法上的AsyncTask的实例

try this: the first call cancel method on the instance of AsyncTask

    findViewById(R.id.cancelButton).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            mTask.cancel(true);
        }
    });

然后你需要添加检查,如果你的异步任务已被取消:

Then you need to add check if your async task has been canceled:

    @Override
    protected String doInBackground(String... params) {

        try{
            // File's binary content
            java.io.File fileContent = new java.io.File(localFilePath);
            final FileInputStream fileStream = new FileInputStream(fileContent);
            InputStreamContent mediaContent = new InputStreamContent(FileInfo.getMime(localFilePath), new BufferedInputStream(fileStream));

            ....

            uploader.setProgressListener(new MediaHttpUploaderProgressListener() {
                void progressChanged(MediaHttpUploader uploader) {
                    if(isCancelled()) {
                        // could not find another way to cancel transferring except that:
                        fileStream.close();
                    }
                    else {
                        mediaHttpUploaderProgressListener(uploader);
                    }
                }
            });

            ....

        } catch (IOException e) {
            Log.e(TAG,"IOEXCEPTION Message:\n"+e.getMessage());
        }

        return null;
    }

希望这会工作。

具有添加以下 - 这不是一个正确的方式如何实现转移操作。
请记住,您的活动可能会停止,甚至毁坏,不是再次创造了,而你的任务是工作。
事件更糟的事情可能会发生。运行时,系统运行的同时转移仍在继续,如果您的应用程序没有在前台任何活动内存的AsyncTask无法prevent摧毁整个应用程序。在这种情况下,转让甚至可能没有完成。

Have to add following - this is not a 'right' way how to implement transferring operations. Keep in mind that your activity may be stopped and even destroyed and than created again while you task is working. Event worse thing may happen. Running AsyncTask cannot prevent destroying the whole app when system runs out of memory while transferring is still going on if your app does not have any activity in foreground. In this case transferring may even not being completed.

这可能是更好的考虑将业务转移到一个专用的服务组件。

It might be better to consider moving transferring operations to a dedicated service component.

这篇关于Android的如何停止/终止/取消媒体上传或下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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