I have not written a blog for more than half a year. I have learned a lot in this half year, but I still lack a summary. The purpose of writing a blog is nothing more than the following: 1. 2. Share what you’ve learned that might help other development partners. 3, to share technology, open source to bring their own a meager force.

DownLoaderManger has been wanting to publish a blog for a long time. When I first got into it, it was really convenient and easy. This blog covers packaging DownLoaderManger and compatibility issues with FileProvider on androidX.

1. Main logic


The main logic

2. Required permissions

The permissions required to complete an APK from download to installation are as follows:

  • To access the network, add the network permission.
  • You need to download files, install APK, and add read and write permissions to files.
  • To install APK, add permission to request application installation.
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
Copy the code

3. DownloadManger for Android


DownloadManger


Android DownLoaderManger is a downloader provided by Android system. It is a system service. The acquisition method is as follows:

var downloadManager: DownloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
Copy the code

The DownloadManager has a Request to build some parameters for the download, some styles for the Notification, and so on. Finally, the download is started using the downloadManager.enqueue () method of DownloadManager. The following is part of the source code (the source address is at the end of the article).

// The actual download logic, which Request uses to build the parameters used by the download. val request = DownloadManager.Request(Uri.parse(url)) request.setTitle(title) request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE or DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) request.setMimeType("application/vnd.android.package-archive") if (TextUtils.isEmpty(title)) { request.setDescription(fileName) } else { request.setDescription(title) } try { if (DownLoaderManager.downloadManager == Return}} Catch (e: Exception) { e.printStackTrace() } request.setDestinationUri(Uri.fromFile(file)) if (DownLoaderManager.downloadManager ! = null) { downloadId = DownLoaderManager.downloadManager!! .enqueue(request) }Copy the code

If the code executes successfully, it is displayed in the notification bar.




Download status

4, download completed listening

DownloaderManger After the file is downloaded, it listens by broadcast, then gets the Uri of the file through downLoadId, and finally installs the software by Uri. There are two ways to register for a broadcast, and here’s the one at AndroidMainfest:

        <receiver android:name=".downloader.DownLoaderBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
            </intent-filter>
        </receiver>
Copy the code
class DownLoaderBroadcastReceiver : BroadcastReceiver() { override fun onReceive(context: Context? , intent: Intent?) {}}Copy the code

5. The difference between File Provider on 7.0 and androidX

File Provider is a new feature in Android 7.0, but there are different differences in X, in fact, it is very easy to modify, only need to modify a parameter. 7.0:

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.fileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
Copy the code

androidX:

        <provider
                android:name="androidx.core.content.FileProvider"
                android:authorities="${applicationId}.fileProvider"
                android:exported="false"
                android:grantUriPermissions="true">
            <meta-data
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/file_paths"/>
        </provider>
Copy the code

Except for the name, everything else is the same.

6, the source code

Note:

  • This is androidX, but the file provider here is different.
  • Kotlin is now the official language for Android development.

Github.com/AxeChen/Dow… If it helps, give it a thumbs up before you leave. 🙂

This article is published in Jane book, other platforms, please indicate the source and author, thank you! Otherwise all for piracy.