Example of Android Breakpoint Download Using Xutils3

  • 2021-11-10 10:49:02
  • OfStack

Using Tools: Android studio 3.0

Usage:

1: Add dependencies in build. gradle (Module: app)


implementaion 'org.xutils:xutils:3.5.1'

2: Create a separate MyApp class to initialize xutils, and the class name can be customized


public class MyApp extends Application {
  @Override
  public void onCreate() {
    super.onCreate();
    x.Ext.init(this);
  }
}

3: Add permissions in AndroidManifest and register MyApp

//Add permissions
< uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/ >
< uses-permission android:name="android.permission.INTERNET"/ >
//Register MyApp

In < application > Add android: name = ". MyApp"

4: Use the download method


public void download(View view){
    String path=et.getText().toString().trim();
    RequestParams params=new RequestParams(path);
    params.setSaveFilePath(Environment.getExternalStorageDirectory()+"/myapp/");
    params.setAutoRename(true);
    x.http().post(params,new Callback.ProgressCallback<File>(){
      @Override
      public void onWaiting() {
 
      }
      @Override
      public void onStarted() {
 
      }
      @Override
      public void onLoading(long total, long current, boolean isDownloading) {
        pd.setMax((int)total);
        pd.setProgress((int)current);
      }
      @Override
      public void onSuccess(File result) {
        Log.i("TEST"," Download complete ");
      }
      @Override
      public void onError(Throwable ex, boolean isOnCallback) {
 
      }
      @Override
      public void onCancelled(CancelledException cex) {
 
      }
      @Override
      public void onFinished() {
 
      }
    });
  }

Extension of knowledge points:

Introduction to xUtils3

xUtils contains orm, http (s), image, view annotations, but it is still lightweight (246K), and has powerful features and easy expansion:

The cornerstone of stability: AbsTask and the callback interface Callback of Unified 1, any exception, even if your callback method implementation has an exception, will enter onError, and onFinished will always let you know that the task is over under any circumstances. Based on the efficient and stable orm tool, http module can more conveniently realize the support of cookie (supporting domain, path, expiry, etc.) and cache (supporting Cache-Control, Last-Modified, ETag, etc.). With the powerful support of http and its download cache, the implementation of image module is quite simple, and it supports the recycling of pictures held by view but removed by Mem Cache, thus reducing the flicker when the page rolls back. The view annotation module provides flexible support for View injection and event binding with just over 400 lines of code, including support for listener with multiple methods.

Related articles: