|
| 1 | +## Android integration |
| 2 | + |
| 3 | +### Required configuration: |
| 4 | + |
| 5 | +* Configure `Application`: |
| 6 | + |
| 7 | +Java: |
| 8 | +```java |
| 9 | +// MyApplication.java (create this file if it doesn't exist in your project) |
| 10 | + |
| 11 | +import io.flutter.app.FlutterApplication; |
| 12 | +import io.flutter.plugin.common.PluginRegistry; |
| 13 | +import io.flutter.plugins.GeneratedPluginRegistrant; |
| 14 | +import vn.hunghd.flutterdownloader.FlutterDownloaderPlugin; |
| 15 | + |
| 16 | +public class MyApplication extends FlutterApplication implements PluginRegistry.PluginRegistrantCallback { |
| 17 | + @Override |
| 18 | + public void registerWith(PluginRegistry registry) { |
| 19 | + // |
| 20 | + // Integration note: |
| 21 | + // |
| 22 | + // In Flutter, in order to work in background isolate, plugins need to register with |
| 23 | + // a special instance of `FlutterEngine` that serves for background execution only. |
| 24 | + // Hence, all (and only) plugins that require background execution feature need to |
| 25 | + // call `registerWith` in this method. |
| 26 | + // |
| 27 | + // The default `GeneratedPluginRegistrant` will call `registerWith` of all plugins |
| 28 | + // integrated in your application. Hence, if you are using `FlutterDownloaderPlugin` |
| 29 | + // along with other plugins that need UI manipulation, you should register |
| 30 | + // `FlutterDownloaderPlugin` and any 'background' plugins explicitly like this: |
| 31 | + // |
| 32 | + // FlutterDownloaderPlugin.registerWith(registry.registrarFor("vn.hunghd.flutterdownloader.FlutterDownloaderPlugin")); |
| 33 | + // |
| 34 | + GeneratedPluginRegistrant.registerWith(registry); |
| 35 | + } |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +Or Kotlin: |
| 40 | +```kotlin |
| 41 | +// MyApplication.kt (create this file if it doesn't exist in your project) |
| 42 | + |
| 43 | +import io.flutter.app.FlutterApplication |
| 44 | +import io.flutter.plugin.common.PluginRegistry |
| 45 | +import io.flutter.plugins.GeneratedPluginRegistrant |
| 46 | +import vn.hunghd.flutterdownloader.FlutterDownloaderPlugin |
| 47 | + |
| 48 | +internal class MyApplication : FlutterApplication(), PluginRegistry.PluginRegistrantCallback { |
| 49 | + override fun registerWith(registry: PluginRegistry) { |
| 50 | + // |
| 51 | + // Integration note: |
| 52 | + // |
| 53 | + // In Flutter, in order to work in background isolate, plugins need to register with |
| 54 | + // a special instance of `FlutterEngine` that serves for background execution only. |
| 55 | + // Hence, all (and only) plugins that require background execution feature need to |
| 56 | + // call `registerWith` in this method. |
| 57 | + // |
| 58 | + // The default `GeneratedPluginRegistrant` will call `registerWith` of all plugins |
| 59 | + // integrated in your application. Hence, if you are using `FlutterDownloaderPlugin` |
| 60 | + // along with other plugins that need UI manipulation, you should register |
| 61 | + // `FlutterDownloaderPlugin` and any 'background' plugins explicitly like this: |
| 62 | + // |
| 63 | + // FlutterDownloaderPlugin.registerWith(registry.registrarFor("vn.hunghd.flutterdownloader.FlutterDownloaderPlugin")) |
| 64 | + // |
| 65 | + GeneratedPluginRegistrant.registerWith(registry) |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +And update `AndroidManifest.xml` |
| 71 | +```xml |
| 72 | +<!-- AndroidManifest.xml --> |
| 73 | +<application |
| 74 | + android:name=".MyApplication" |
| 75 | + ....> |
| 76 | +``` |
| 77 | + |
| 78 | +* In order to handle click action on notification to open the downloaded file on Android, you need to add some additional configurations. Add the following codes to your `AndroidManifest.xml`: |
| 79 | + |
| 80 | +````xml |
| 81 | +<provider |
| 82 | + android:name="vn.hunghd.flutterdownloader.DownloadedFileProvider" |
| 83 | + android:authorities="${applicationId}.flutter_downloader.provider" |
| 84 | + android:exported="false" |
| 85 | + android:grantUriPermissions="true"> |
| 86 | + <meta-data |
| 87 | + android:name="android.support.FILE_PROVIDER_PATHS" |
| 88 | + android:resource="@xml/provider_paths"/> |
| 89 | +</provider> |
| 90 | +```` |
| 91 | + |
| 92 | +**Note:** |
| 93 | + - You have to save your downloaded files in external storage (where the other applications have permission to read your files) |
| 94 | + - The downloaded files are only able to be opened if your device has at least an application that can read these file types (mp3, pdf, etc) |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +### Optional configuration: |
| 99 | + |
| 100 | +* **Configure maximum number of concurrent tasks:** the plugin depends on `WorkManager` library and `WorkManager` depends on the number of available processor to configure the maximum number of tasks running at a moment. You can setup a fixed number for this configuration by adding following codes to your `AndroidManifest.xml`: |
| 101 | + |
| 102 | +````xml |
| 103 | + <provider |
| 104 | + android:name="androidx.work.impl.WorkManagerInitializer" |
| 105 | + android:authorities="${applicationId}.workmanager-init" |
| 106 | + android:enabled="false" |
| 107 | + android:exported="false" /> |
| 108 | + |
| 109 | + <provider |
| 110 | + android:name="vn.hunghd.flutterdownloader.FlutterDownloaderInitializer" |
| 111 | + android:authorities="${applicationId}.flutter-downloader-init" |
| 112 | + android:exported="false"> |
| 113 | + <!-- changes this number to configure the maximum number of concurrent tasks --> |
| 114 | + <meta-data |
| 115 | + android:name="vn.hunghd.flutterdownloader.MAX_CONCURRENT_TASKS" |
| 116 | + android:value="5" /> |
| 117 | + </provider> |
| 118 | + ```` |
| 119 | + |
| 120 | +* **Localize notification messages:** you can localize notification messages of download progress by localizing following messages. (you can find the detail of string localization in Android in this [link][4]) |
| 121 | + |
| 122 | +````xml |
| 123 | +<string name="flutter_downloader_notification_started">Download started</string> |
| 124 | +<string name="flutter_downloader_notification_in_progress">Download in progress</string> |
| 125 | +<string name="flutter_downloader_notification_canceled">Download canceled</string> |
| 126 | +<string name="flutter_downloader_notification_failed">Download failed</string> |
| 127 | +<string name="flutter_downloader_notification_complete">Download complete</string> |
| 128 | +<string name="flutter_downloader_notification_paused">Download paused</string> |
| 129 | +```` |
| 130 | + |
| 131 | +* **PackageInstaller:** in order to open APK files, your application needs `REQUEST_INSTALL_PACKAGES` permission. Add following codes in your `AndroidManifest.xml`: |
| 132 | + |
| 133 | +````xml |
| 134 | +<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" /> |
| 135 | +```` |
0 commit comments