在分段上传请求中实现ProgressDialog

在分段上传请求中实现ProgressDialog,第1张

分段上传请求中实现ProgressDialog

上这堂课:

public class SingleUploadBroadcastReceiver extends UploadServiceBroadcastReceiver {    public interface Delegate {        void onProgress(int progress);        void onProgress(long uploadedBytes, long totalBytes);        void onError(Exception exception);        void onCompleted(int serverResponseCode, byte[] serverResponseBody);        void onCancelled();    }    private String mUploadID;    private Delegate mDelegate;    public void setUploadID(String uploadID) {        mUploadID = uploadID;    }    public void setDelegate(Delegate delegate) {        mDelegate = delegate;    }    @Override    public void onProgress(String uploadId, int progress) {        if (uploadId.equals(mUploadID) && mDelegate != null) { mDelegate.onProgress(progress);        }    }    @Override    public void onProgress(String uploadId, long uploadedBytes, long totalBytes) {        if (uploadId.equals(mUploadID) && mDelegate != null) { mDelegate.onProgress(uploadedBytes, totalBytes);        }    }    @Override    public void onError(String uploadId, Exception exception) {        if (uploadId.equals(mUploadID) && mDelegate != null) { mDelegate.onError(exception);        }    }    @Override    public void onCompleted(String uploadId, int serverResponseCode, byte[] serverResponseBody) {        if (uploadId.equals(mUploadID) && mDelegate != null) { mDelegate.onCompleted(serverResponseCode, serverResponseBody);        }    }    @Override    public void onCancelled(String uploadId) {        if (uploadId.equals(mUploadID) && mDelegate != null) { mDelegate.onCancelled();        }    }}

然后在您的活动中:

public class YourActivity extends Activity implements SingleUploadBroadcastReceiver.Delegate {    private static final String TAG = "AndroidUploadService";    private final SingleUploadBroadcastReceiver uploadReceiver =        new SingleUploadBroadcastReceiver();    @Override    protected void onResume() {        super.onResume();        uploadReceiver.register(this);    }    @Override    protected void onPause() {        super.onPause();        uploadReceiver.unregister(this);    }    public void uploadMultipart(final Context context) {        try { String uploadId = UUID.randomUUID().toString(); uploadReceiver.setDelegate(this); uploadReceiver.setUploadID(uploadId); new MultipartUploadRequest(context, uploadId, "http://upload.server.com/path")     .addFileToUpload("/absolute/path/to/your/file", "your-param-name")     .setNotificationConfig(new UploadNotificationConfig())     .setMaxRetries(2)     .startUpload();        } catch (Exception exc) { Log.e(TAG, exc.getMessage(), exc);        }    }    @Override    public void onProgress(int progress) {        //your implementation    }    @Override    public void onProgress(long uploadedBytes, long totalBytes) {        //your implementation    }    @Override    public void onError(Exception exception) {        //your implementation    }    @Override    public void onCompleted(int serverResponseCode, byte[] serverResponseBody) {        //your implementation    }    @Override    public void onCancelled() {        //your implementation    }}

现在,您将在成功上传/失败后触发适当的回调。

资源



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5439557.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存