Android 下载完成dropbox核心API时通知

Android 下载完成dropbox核心API时通知,android,dropbox,dropbox-api,Android,Dropbox,Dropbox Api,我正在制作一个模块,可以将dropbox上apps文件夹中的所有文件下载到设备上 现在,我正在使用CoreAPI下载一个文件。一切正常。但是对于多个文件,我不知道当前文件何时完成下载以转到下一个文件 这是我的下载代码: final String cachePath = FileHandler.ROOT_LOCAL_FOLDER_DIR + "/" + mFile.getFileName(); try { mFos = new FileOut

我正在制作一个模块,可以将dropbox上apps文件夹中的所有文件下载到设备上

现在,我正在使用CoreAPI下载一个文件。一切正常。但是对于多个文件,我不知道当前文件何时完成下载以转到下一个文件

这是我的下载代码:

final String cachePath = FileHandler.ROOT_LOCAL_FOLDER_DIR + "/" + mFile.getFileName();
            try {
                mFos = new FileOutputStream(cachePath);
            } catch (FileNotFoundException e) {
                mErrorMsg = "Couldn't create a local file to store the image";
                return false;
            }

            ProgressListener mProgressLisenter = new ProgressListener() {

                @Override
                public void onProgress(long arg0, long arg1) {
                    // TODO Auto-generated method stub
                    tmpFile = new File(cachePath);
                    OnDownloadProgressDropboxChecked(FileHandler.PROGRESS_STATUS_ONPROGRESS, (int) (arg0 * 100 / arg1));
                }

                @Override
                public long progressInterval() {
                    return 100;
                }
            };
            mDownloaded = mApi.getFile(mFile.getFileId(), null, mFos, mProgressLisenter);
                    OnDownloadDropboxChecked(TRUE, "Download complete");
        } catch (DropboxUnlinkedException e) {
            mErrorMsg = "Unlinked";
        } catch (DropboxPartialFileException e) {
            // We canceled the operation
            mErrorMsg = "Download canceled";

        } catch (DropboxServerException e) {
        }
}
我试过:

  • 捕获进度下载,当达到100%时通知-这样,dropbox的OnProgress不会立即显示,有时也不会达到100%
  • 把它放在一个异步任务中——没用
我现在不知所措,任何进步都对我有好处


编辑:
解决方案是在getFile()下放置一些触发器,然后它就可以工作了

这是我的完整课程:

public class DownloadFile extends AsyncTask<Void, Long, Boolean> implements DConst {

    Context act;
    private DropboxAPI<?> mApi;
    // private String mPath;

    private FileOutputStream mFos;

    private boolean mCanceled;
    private Long mFileLen;
    private String mErrorMsg;
    private String mFileName;
    String path;
    DFile mFile;
    DropboxAPI.DropboxFileInfo mDownloaded;

    @SuppressWarnings("deprecation")
    public DownloadFile(Context act, DropboxAPI<?> api, DFile mDFile) {
        // We set the context this way so we don't accidentally leak activities
        this.act = act;
        mApi = api;
        mFile = mDFile;
        path = mDFile.getFileId();
        mFileName = mDFile.getFileName();
        OnDownloadProgressDropboxChecked(FileHandler.PROGRESS_STATUS_START, 0);
    }

    File tmpFile;

    @Override
    protected Boolean doInBackground(Void... params) {
        try {

            final String cachePath = FileHandler.ROOT_LOCAL_FOLDER_DIR + "/" + mFile.getFileName();
            try {
                mFos = new FileOutputStream(cachePath);
            } catch (FileNotFoundException e) {
                mErrorMsg = "Couldn't create a local file to store the image";
                return false;
            }

            ProgressListener mProgressLisenter = new ProgressListener() {

                @Override
                public void onProgress(long arg0, long arg1) {
                    // TODO Auto-generated method stub
                    tmpFile = new File(cachePath);
                    OnDownloadProgressDropboxChecked(FileHandler.PROGRESS_STATUS_ONPROGRESS, (int) (arg0 * 100 / arg1));
                    Log.d("Dolphin got interval", String.valueOf(tmpFile.length() + " - " + arg0 + " - " + arg1));
                }

                @Override
                public long progressInterval() {
                    return 100;
                }
            };
            mDownloaded = mApi.getFile(mFile.getFileId(), null, mFos, mProgressLisenter);

        } catch (DropboxUnlinkedException e) {
            mErrorMsg = "Unlinked";
        } catch (DropboxPartialFileException e) {
            // We canceled the operation
            mErrorMsg = "Download canceled";

        } catch (DropboxServerException e) {
            // Server-side exception. These are examples of what could happen,
            // but we don't do anything special with them here.
            if (e.error == DropboxServerException._304_NOT_MODIFIED) {
                // won't happen since we don't pass in revision with metadata
            } else if (e.error == DropboxServerException._401_UNAUTHORIZED) {
                // Unauthorized, so we should unlink them. You may want to
                // automatically log the user out in this case.
            } else if (e.error == DropboxServerException._403_FORBIDDEN) {
                // Not allowed to access this
            } else if (e.error == DropboxServerException._404_NOT_FOUND) {
                // path not found (or if it was the thumbnail, can't be
                // thumbnailed)
            } else if (e.error == DropboxServerException._406_NOT_ACCEPTABLE) {
                // too many entries to return
            } else if (e.error == DropboxServerException._415_UNSUPPORTED_MEDIA) {
                // can't be thumbnailed
            } else if (e.error == DropboxServerException._507_INSUFFICIENT_STORAGE) {
                // user is over quota
            } else {
                // Something else
            }
            // This gets the Dropbox error, translated into the user's language
            mErrorMsg = e.body.userError;
            if (mErrorMsg == null) {
                mErrorMsg = e.body.error;
            }
        } catch (DropboxIOException e) {
            // Happens all the time, probably want to retry automatically.
            mErrorMsg = "Network error.  Try again.";
        } catch (DropboxParseException e) {
            // Probably due to Dropbox server restarting, should retry
            mErrorMsg = "Dropbox error.  Try again.";
        } catch (DropboxException e) {
            // Unknown error
            mErrorMsg = "Unknown error.  Try again.";
        } finally {
            if (mFos != null) {
                try {
                    mFos.close();
                    return true;
                } catch (IOException e) {
                }
            }
        }
        return false;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        OnDownloadProgressDropboxChecked(FileHandler.PROGRESS_STATUS_FINISH, 0);
        if (result) {
            OnDownloadDropboxChecked(TRUE, "Download complete");
        } else {
            OnDownloadDropboxChecked(FALSE, mErrorMsg);
        }
    }

    OnAsyncDownloadListener onAsyncDownloadListener;
    OnAsyncDownloadProgressListener onAsyncDownloadProgressListener;

    private void OnDownloadDropboxChecked(int res, String messenger) {
        if (onAsyncDownloadListener != null) {
            onAsyncDownloadListener.OnAsyncDownload(res, messenger);
        }
    }

    private void OnDownloadProgressDropboxChecked(int status, int percent) {
        if (onAsyncDownloadProgressListener != null) {
            onAsyncDownloadProgressListener.OnAsyncDownloadProgress(status, percent);
        }
    }

    public void setOnAsyncDownloadListener(OnAsyncDownloadListener listener) {
        onAsyncDownloadListener = listener;
    }

    public void setOnAsyncDownloadProgressListener(OnAsyncDownloadProgressListener listener) {
        onAsyncDownloadProgressListener = listener;
    }

    public interface OnAsyncDownloadListener {
        public abstract void OnAsyncDownload(int res, String messenger);
    }

    public interface OnAsyncDownloadProgressListener {
        public abstract void OnAsyncDownloadProgress(int status, int percent);
    }
}
公共类下载文件扩展AsyncTask实现DConst{
上下文行为;
私有DropboxAPI mApi;
//私有字符串mPath;
私有文件输出流MFO;
私有布尔mcanced;
私人长姆费伦;
私有字符串merrormg;
私有字符串mFileName;
字符串路径;
数据文件;
DropboxAPI.DropboxFileInfo mdownload;
@抑制警告(“弃用”)
公共下载文件(上下文法案、DropboxAPI、DFile mDFile){
//我们以这种方式设置上下文,以免意外泄漏活动
this.act=act;
mApi=api;
mFile=mDFile;
path=mDFile.getFileId();
mFileName=mDFile.getFileName();
OnDownloadProgressDropboxChecked(FileHandler.PROGRESS\u STATUS\u START,0);
}
文件tmpFile;
@凌驾
受保护的布尔doInBackground(Void…params){
试一试{
最终字符串cachePath=FileHandler.ROOT_LOCAL_FOLDER_DIR+“/”+mFile.getFileName();
试一试{
mFos=新文件输出流(缓存路径);
}catch(filenotfounde异常){
mErrorMsg=“无法创建本地文件来存储图像”;
返回false;
}
ProgressListener mProgressLisenter=新建ProgressListener(){
@凌驾
public void onProgress(长arg0、长arg1){
//TODO自动生成的方法存根
tmpFile=新文件(缓存路径);
OnDownloadProgressDropboxChecked(FileHandler.PROGRESS_STATUS_ONPROGRESS,(int)(arg0*100/arg1));
Log.d(“Dolphin Get interval”,String.valueOf(tmpFile.length()+“-”+arg0+“-”+arg1));
}
@凌驾
公共长时间间隔(){
返回100;
}
};
mdownload=mApi.getFile(mFile.getFileId(),null,mFos,mProgressLisenter);
}捕获(DropboxUnlinkedException){
mErrorMsg=“未链接”;
}捕获(DropboxPartialFileE异常){
//我们取消了手术
mErrorMsg=“下载已取消”;
}捕获(DropboxServerException e){
//服务器端异常。这些是可能发生的情况的示例,
//但我们在这里对他们没有什么特别的处理。
if(e.error==DropboxServerException.\u 304\u未修改){
//不会发生这种情况,因为我们没有传递带有元数据的修订
}否则如果(e.error==DropboxServerException.\u 401\u未经授权){
//未经授权,所以我们应该取消它们的链接。您可能想要
//在这种情况下,自动注销用户。
}否则如果(e.error==DropboxServerException.\u 403\u禁止){
//不允许访问此文件
}else if(e.error==DropboxServerException.\u 404\u未找到){
//未找到路径(或者如果是缩略图,则无法
//拇指钉)
}否则如果(e.error==DropboxServerException.\u 406\u不可接受){
//返回的条目太多
}else if(e.error==DropboxServerException.\u 415\u不支持的\u媒体){
//不能用拇指钉住
}否则如果(e.error==DropboxServerException.\u 507\u存储不足){
//用户超过配额
}否则{
//别的
}
//这将获取Dropbox错误,并将其翻译成用户的语言
mErrorMsg=e.body.userError;
if(mErrorMsg==null){
mErrorMsg=e.body.error;
}
}捕获(DropboxIOE异常){
//经常发生,可能要自动重试。
mErrorMsg=“网络错误。请重试。”;
}捕获(DropboxParse异常){
//可能是由于Dropbox服务器重新启动,应该重试
mErrorMsg=“Dropbox错误。请重试。”;
}捕获(DropBoxexException e){
//未知错误
mErrorMsg=“未知错误。请重试。”;
}最后{
如果(mFos!=null){
试一试{
mFos.close();
返回true;
}捕获(IOE异常){
}
}
}
返回false;
}
@凌驾
受保护的void onPostExecute(布尔结果){
OnDownloadProgressDropboxChecked(FileHandler.PROGRESS\u STATUS\u FINISH,0);
如果(结果){
OnDownloadDropboxChecked(TRUE,“下载完成”);
}否则{
OnDownloadDropboxChecked(错误,错误消息);
}
}
OnAsyncDownloadListener OnAsyncDownloadListener;
OnAsyncDownloadProgressListener OnAsyncDownloadProgressListener;
私有void OnDownloadDropboxChecked(int-res,字符串信使){
if(onAsyncDownloadListener!=null){
onAsyncDownloadListener.OnAsyncDownload(res,messenger);
}
}
私有void OnDownloadProgressDropboxChecked(整数状态,整数百分比){
if(onAsyncDownloadProgressListener!=null){
女歌手