The origin of

This article focuses on the use of the open source project Aria. The Aria project was born out of a need for file download management during 15 years of work. At that time, I was suffering from the pain of downloading. From then on, I was inspired to write a simple and easy to use, stable and efficient download framework.

The following is an example of how Aria can be used to develop all the features in an image in a very short time with very little code.





Aria sample

Writing layout files

<?xml version="1.0" encoding="utf-8"? >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent">

  <com.arialyy.simple.widget.HorizontalProgressBarWithNumber
      android:id="@+id/progressBar"
      android:layout_width="wrap_content"
      android:layout_height="20dp"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_alignParentTop="true"
      android:layout_margin="16dp"
      android:layout_toLeftOf="@+id/size"
      android:max="100"
      style="? android:attr/progressBarStyleHorizontal"/>

  <TextView
      android:id="@+id/size"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"
      android:layout_alignTop="@+id/progressBar"
      android:layout_marginRight="16dp"
      android:text="0mb"
      android:textSize="16sp"/>

  <LinearLayout
      android:id="@+id/handle_bar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_below="@+id/progressBar"
      android:orientation="horizontal">

    <TextView
        android:id="@+id/speed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:text="0kb/s"
        android:textColor="@android:color/black"/>

    <Button
        android:id="@+id/start"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="onClick"
        android:text="Start"
        style="? buttonBarButtonStyle"/>

    <Button
        android:id="@+id/stop"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="onClick"
        android:text="Pause"
        style="? buttonBarButtonStyle"/>

    <Button
        android:id="@+id/cancel"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="onClick"
        android:text="Delete task"
        style="? buttonBarButtonStyle"/>
  </LinearLayout>

</RelativeLayout>Copy the code

Call Aria’s API for download

@OnClick({ R.id.start, R.id.stop, R.id.cancel }) public void onClick(View view) {
   switch (view.getId()) {
     case R.id.start:
       Aria.download(this)
           .load(DOWNLOAD_URL)
           .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk")
           .start();
       break;
     case R.id.stop:
       Aria.download(this).load(DOWNLOAD_URL).pause();
       break;
     case R.id.cancel:
       Aria.download(this).load(DOWNLOAD_URL).cancel();
       break; }}Copy the code

In the Aria download module, the download link is the unique identifier of the download task, and the operations to start, pause, resume, and cancel the download of a task require DOWNLOAD_URL support. Also, in Aira, you can call the start() method to start or resume the download. For convenience, you can also call aria.download (this).load(DOWNLOAD_URL).resume(); Implement recovery download.

Aria provides a large number of apis. In addition to the above commonly used apis, you can also use the Aria API;

Download event fetching

In the previous article, we have implemented the operation of downloading files in Android. Yes, with Aria, you can implement complex resumable functions with one line of code. In addition to normal file requirements, sometimes we also need to obtain the download status of the file to update the interface display. In Aria, you can easily retrieve as many events as you want using the Aria event listener. For example:

  • Pause, resume, complete, fail, cancel download, etc
  • A line of code gets the download speed of the current task
  • A line of code gets the percentage of progress of the current task
  • One line of code gets the file size of the current task…

    private class MySchedulerListener extends Aria.DownloadSchedulerListener {
    
      @Override public void onTaskStart(DownloadTask task) {
        mSize.setText(task.getConvertFileSize());
      }
    
      @Override public void onTaskStop(DownloadTask task) {
        Toast.makeText(MainActivity.this."Stop downloading", Toast.LENGTH_SHORT).show();
      }
    
      @Override public void onTaskCancel(DownloadTask task) {
        Toast.makeText(MainActivity.this."Cancel download", Toast.LENGTH_SHORT).show();
      }
    
      @Override public void onTaskFail(DownloadTask task) {
        Toast.makeText(MainActivity.this."Download failed", Toast.LENGTH_SHORT).show();
      }
    
      @Override public void onTaskComplete(DownloadTask task) {
        Toast.makeText(MainActivity.this."Download completed", Toast.LENGTH_SHORT).show();
      }
    
      @Override public void onTaskRunning(DownloadTask task) {
        // The unit conversion switch needs to be turned on in the aria_config.xml configuration file
        / / https://github.com/AriaLyy/Aria# profile SettingsmSpeed.setText(task.getConvertSpeed()); mPb.setProgress(task.getPercent()); }}Copy the code

    Only common download events are listed above; see the Aria Download Event Listener description for more download status events

Register the newly created event listener in Aria

Now that you’ve created event listeners, you need to register them with Aria before they take effect

@Override protected void onResume(a) {
    super.onResume();
    Aria.download(this).addSchedulerListener(new MySchedulerListener());
  }Copy the code

The final result





The final result

final

That’s all the code you need to download files using Aria, but Aria is really simple. With very little code, you can implement complex file multi-threaded breakpoint continuation. In addition, Aria also supports multi-threaded configuration. How many threads to download a file is up to you, see Multi-threaded configuration. Aria supports automatic task scheduling by default, so you don’t need to worry about suspending, resuming, completing, failing, canceling, etc.

Aria download

This article demo download address