最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Flutter下載更新App的方法示例

 更新時間:2019年07月17日 08:33:47   作者:破森圖  
這篇文章主要介紹了Flutter下載更新App的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

1. 說明

iOS 和Android 更新是完全不一樣的。

iOS 只能跳轉(zhuǎn)到 AppStore,比較好實現(xiàn)

Android則需要下載apk包,由于Android機型較多,這里我們用 dart 連接第三方(這里)的原生 android 下載庫。

更新界面和下載更新分開處理的。

iOS 沒得下載進度這一說,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實現(xiàn)

2.1.1 在 android/app/build.gradle 文件添加下載庫

dependencies {
  // 只復(fù)制這一行
  implementation 'com.king.app:app-updater:1.0.4-androidx'
}

2.1.2 在 AndroidManifest.xml添加存儲權(quán)限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

2.1.3 在 android 項目中編寫插件

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錯誤", o);
   return;
  }
  if (!o.toString().startsWith("http")){
   eventSink.error(TAG, "URL錯誤", 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 中注冊插件

// 注冊更新組件 在onCreate方法中
UpdateVersionPlugin.registerWith(registrarFor("iwubida.com/update_version"));

我們需要獲取到下載進度,所以我們采用EventChannel來持續(xù)單向通訊。

2.3 dart端實現(xiàn)

static const channelName = 'plugins.iwubida.com/update_version';
 static const stream = const EventChannel(channelName);
 // 進度訂閱
 StreamSubscription downloadSubscription;
 int percent = 0;
 
  // 開始下載
 void _startDownload() {
  if (downloadSubscription == null) {
   downloadSubscription = stream
     .receiveBroadcastStream(widget.data.apkUrl)
     .listen(_updateDownload);
  }
 }

 // 停止監(jiān)聽進度
 void _stopDownload() {
  if (downloadSubscription != null) {
   downloadSubscription.cancel();
   downloadSubscription = null;
   percent = 0;
  }
 }

 // 進度下載
 void _updateDownload(data) {
  int progress = data["percent"];
  if (progress != null) {
   setState(() {
    percent = progress;
   });
  }
 }
 

2.4 其它

另外 Android 還有權(quán)限申請的問題。可以參考下面項目中的代碼。

https://github.com/xushengjiang0/flutter_yuedu

dart 代碼: lib/widget/update_version.dart

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

朝阳市| 安乡县| 仙桃市| 攀枝花市| 江孜县| 英吉沙县| 临夏市| 鸡泽县| 交城县| 容城县| 吉林省| 凤山市| 富宁县| 新干县| 崇明县| 达尔| 天柱县| 类乌齐县| 丘北县| 颍上县| 镇沅| 西林县| 黑山县| 西贡区| 金堂县| 木兰县| 墨竹工卡县| 南京市| 佛坪县| 江源县| 阿克苏市| 南澳县| 屏南县| 历史| 金沙县| 五莲县| 昆明市| 平谷区| 乌拉特前旗| 黄浦区| 会同县|