Flutter下載更新App的方法示例
1. 說明
iOS 和Android 更新是完全不一樣的。
iOS 只能跳轉(zhuǎn)到 AppStore,比較好實(shí)現(xiàn)
Android則需要下載apk包,由于Android機(jī)型較多,這里我們用 dart 連接第三方(這里)的原生 android 下載庫(kù)。
更新界面和下載更新分開處理的。
iOS 沒得下載進(jìn)度這一說,Android 則有。
2. 代碼
2.1 iOS 直接采用url_launcher就可以了
if (Platform.isIOS) { final url = "https://itunes.apple.com/cn/app/id1380512641"; // id 后面的數(shù)字換成自己的應(yīng)用 id 就行了 if (await canLaunch(url)) { await launch(url, forceSafariVC: false); } else { throw 'Could not launch $url'; } }
2.1 Android實(shí)現(xiàn)
2.1.1 在 android/app/build.gradle 文件添加下載庫(kù)
dependencies { // 只復(fù)制這一行 implementation 'com.king.app:app-updater:1.0.4-androidx' }
2.1.2 在 AndroidManifest.xml添加存儲(chǔ)權(quán)限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
2.1.3 在 android 項(xiàng)目中編寫插件
package com.iwubida.flutter_yuedu.plugins; import android.content.Context; import android.util.Log; import com.king.app.updater.AppUpdater; import com.king.app.updater.callback.UpdateCallback; import java.io.File; import java.util.HashMap; import java.util.Map; import io.flutter.plugin.common.EventChannel; import io.flutter.plugin.common.PluginRegistry.Registrar; /** UpdateVersionPlugin */ public class UpdateVersionPlugin implements EventChannel.StreamHandler { private static String TAG = "MY_UPDATE"; private static Context context; public UpdateVersionPlugin(Context context) { this.context = context; } /** Plugin registration. */ public static void registerWith(Registrar registrar) { final EventChannel channel = new EventChannel(registrar.messenger(), "plugins.iwubida.com/update_version"); channel.setStreamHandler(new UpdateVersionPlugin(registrar.context())); } @Override public void onListen(Object o, EventChannel.EventSink eventSink) { if (o.toString().length() < 5) { eventSink.error(TAG, "URL錯(cuò)誤", o); return; } if (!o.toString().startsWith("http")){ eventSink.error(TAG, "URL錯(cuò)誤", o); } AppUpdater update = new AppUpdater(context,o.toString()).setUpdateCallback(new UpdateCallback() { Map data = new HashMap<String, Object>(); // 發(fā)送數(shù)據(jù)到 Flutter private void sendData() { eventSink.success(data); } @Override public void onDownloading(boolean isDownloading) { } @Override public void onStart(String url) { data.put("start", true); data.put("cancel", true); data.put("done", true); data.put("error", false); data.put("percent", 1); sendData(); } @Override public void onProgress(int progress, int total, boolean isChange) { int percent = (int)(progress * 1.0 / total * 100); if (isChange && percent > 0) { data.put("percent", percent); sendData(); } } @Override public void onFinish(File file) { data.put("done", true); sendData(); } @Override public void onError(Exception e) { data.put("error", e.toString()); sendData(); } @Override public void onCancel() { data.put("cancel", true); sendData(); } }); update.start(); } @Override public void onCancel(Object o) { Log.i(TAG, "取消下載-集成的第三方下載沒有提供取消方法"); } }
2.1.4 在 MainActivity 中注冊(cè)插件
// 注冊(cè)更新組件 在onCreate方法中 UpdateVersionPlugin.registerWith(registrarFor("iwubida.com/update_version"));
我們需要獲取到下載進(jìn)度,所以我們采用EventChannel來持續(xù)單向通訊。
2.3 dart端實(shí)現(xiàn)
static const channelName = 'plugins.iwubida.com/update_version'; static const stream = const EventChannel(channelName); // 進(jìn)度訂閱 StreamSubscription downloadSubscription; int percent = 0; // 開始下載 void _startDownload() { if (downloadSubscription == null) { downloadSubscription = stream .receiveBroadcastStream(widget.data.apkUrl) .listen(_updateDownload); } } // 停止監(jiān)聽進(jìn)度 void _stopDownload() { if (downloadSubscription != null) { downloadSubscription.cancel(); downloadSubscription = null; percent = 0; } } // 進(jìn)度下載 void _updateDownload(data) { int progress = data["percent"]; if (progress != null) { setState(() { percent = progress; }); } }
2.4 其它
另外 Android 還有權(quán)限申請(qǐng)的問題??梢詤⒖枷旅骓?xiàng)目中的代碼。
https://github.com/xushengjiang0/flutter_yuedu
dart 代碼: lib/widget/update_version.dart
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Bitmap和Drawable的對(duì)比
這篇文章主要介紹了Android Bitmap和Drawable的對(duì)比的相關(guān)資料,需要的朋友可以參考下2017-05-05Android 開發(fā)之dataBinding與ListView及事件
這篇文章主要介紹了Android 開發(fā)之dataBinding與ListView及事件的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10Android實(shí)現(xiàn)雙擊TitleBar回頂部的功能示例代碼
一個(gè)簡(jiǎn)單易用的導(dǎo)航欄TitleBar,可以輕松實(shí)現(xiàn)IOS導(dǎo)航欄的各種效果,下面這篇文章主要給大家介紹了關(guān)于Android如何實(shí)現(xiàn)雙擊TitleBar回頂部功能的相關(guān)資料,文中給出了詳細(xì)的示例代碼,需要的朋友可以參考借鑒,下面來一起看看吧。2017-09-09Android自定義View繪制貝塞爾曲線中小紅點(diǎn)的方法
貝塞爾曲線的本質(zhì)是通過數(shù)學(xué)計(jì)算的公式來繪制平滑的曲線,分為一階,二階,三階及多階。但是這里不講數(shù)學(xué)公式和驗(yàn)證,那些偉大的數(shù)學(xué)家已經(jīng)證明過了,所以就只講講Android開發(fā)中的運(yùn)用吧2023-02-02android Launcher AppWidget添加步驟介紹
大家好,本篇文章主要講的是android Launcher AppWidget添加步驟介紹,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2022-01-01Android仿微信圖片上傳帶加號(hào)且超過最大數(shù)隱藏功能
這篇文章給大家分享android仿照微信空間上傳圖片,顯示圖片數(shù)量以及超過最大,上傳按鈕隱藏功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2018-03-03android?原生安全音量配置邏輯設(shè)計(jì)詳解
這篇文章主要為大家介紹了android?原生安全音量配置邏輯設(shè)計(jì)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Android利用ScaleTransition實(shí)現(xiàn)吹氣球動(dòng)畫
這篇文章主要為大家介紹了如何將利用ScaleTransition實(shí)現(xiàn)一個(gè)吹氣球的動(dòng)畫,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2022-04-04