当前位置: 首页 > news >正文

园林设计公司网站软件是怎么制作的

园林设计公司网站,软件是怎么制作的,紫网站建设,海外域名注册平台flutter开发实战-应用更新apk下载、安装apk、启动应用实现 在开发过程中,经常遇到需要更新下载新版本的apk文件,之后进行应用更新apk下载、安装apk、启动应用。我们在flutter工程中实现下载apk,判断当前版本与需要更新安装的版本进行比对判断…

flutter开发实战-应用更新apk下载、安装apk、启动应用实现

在开发过程中,经常遇到需要更新下载新版本的apk文件,之后进行应用更新apk下载、安装apk、启动应用。我们在flutter工程中实现下载apk,判断当前版本与需要更新安装的版本进行比对判断,通过判断VersionCode来确定下载新版版APK

一、应用更新apk下载

当应用需要更新的时候,我们需要判断版本号,在flutter工程中versionCode是工程中的pubspec.yaml中的version确定的。

如version: 1.0.0+1

version为1.0.0,versionCode为1

需要我们获取接口,需要判断的就是versionCode确定是否需要下载apk。

1.1、获取新版本地址接口

获取新版本的接口使用的是Dio库。dio 是一个强大的 Dart HTTP 请求库,支持全局配置、Restful API、FormData、拦截器、 请求取消、Cookie 管理、文件上传/下载、超时以及自定义适配器等。

这里的请求为GET请求,

Response? response = await dio.get(requestUrl,queryParameters: params,options: Options(contentType: Headers.jsonContentType));

我这里就不写请求的逻辑了。
根据请求,获取到了

// 获取检查版本

Future<void> checkVersion() async {var params = {};ApiRepository.checkVersion(params: params,success: (response) {// {"version":"2","url":"http://wwww.laileshuo.com/download/myapp_v1.0.0_release.apk"}var object = response.object;if (object != null && (object is Map) && object.isNotEmpty) {String? versionCode = object['versionCode'];String? url = object['url'];// 判断是否需要下载更新String versionCodeStr = "";if (version != null) {versionCodeStr = "${versionCode}";}checkAppVersionUpdate(versionCodeStr: versionCodeStr, apkUrl: url);}print("checkVersion params:${params}, object:${object.toString()}");},failure: (error) {print("checkVersion params:${params}, error:${error.toString()}");});}

通过检查新版本接口获取到了url及versionCode,这里的versionCode和pubspec.yaml的进行比较看是否需要下载apk。

判断下载apk

Future<void> checkAppVersionUpdate({String? versionCodeStr, String? apkUrl}) async {try {if (versionCodeStr != null &&apkUrl != null &&versionCodeStr.isNotEmpty &&apkUrl.isNotEmpty) {String curVersionCodeStr = await PlatformUtils.getBuildNum();int versionCode = int.parse(versionCodeStr);int curVersionCode = int.parse(curVersionCodeStr);if (versionCode > curVersionCode) {// 需要更新的版本code,大于当前的版本才更新}}} catch (e) {print("appVersionUpdate apkUrl:${apkUrl}, version:${version}, exception:${e.toString()}");}}

1.2、下载Apk

在判断需要更新的时候,我们需要下载新版本的apk。下载的库我们使用的也是Dio。

下载的代码可参考https://blog.csdn.net/gloryFlow/article/details/131658621

当获取到新版的下载地址url时候,需要下载apk

void downApk(String url, String saveDestPath) {HttpApi().doDownload(url, saveDestPath, cancelToken: CancelToken(),progress: (int received, int total) {// 下载进度setState(() {_downloadRatio = (received / total);if (_downloadRatio == 1) {_downloading = false;}_downloadIndicator = (_downloadRatio * 100).toStringAsFixed(2) + '%';});}, completion: () {// 下载成功FlutterLoadingHud.showToast(message: "\"下载完成\"");}, failure: (error) {// 下载出错FlutterLoadingHud.showToast(message: error.message);});
}

下载完成后可以执行安装并且启动操作了。

二、APK安装及启动

APK安装及启动需要原生插件来实现。

2.1、创建原生插件flutter_package_manager

创建flutter plugin,我使用的工具是Android studio。

配置如下内容:

  • Project name
  • Project location
  • Description
  • Project type: Plugin
  • Android language
  • iOS language
  • Platforms

如图所示

在这里插入图片描述

我们需要实现installThenStart

/// An implementation of [FlutterPackageManagerPlatform] that uses method channels.
class MethodChannelFlutterPackageManager extends FlutterPackageManagerPlatform {/// The method channel used to interact with the native platform.final methodChannel = const MethodChannel('flutter_package_manager');Future<String?> getPlatformVersion() async {final version = await methodChannel.invokeMethod<String>('getPlatformVersion');return version;}Future<void> installThenStart(String apkFilePath, String activity) async {final result = await methodChannel.invokeMethod<void>('installThenStart');return result;}
}

可以看到定义了installThenStart,需要apkFilePath与activity作为参数。

在Android端实现,由于我这边需要静默安装(apk在后台安装,不出现安装界面的提示)

public class FlutterPackageManager implements MethodCallHandler {private static final String TAG = "FlutterPackageManager";private final Registrar registrar;/*** Plugin registration.*/public static void registerWith(Registrar registrar) {final MethodChannel channel = new MethodChannel(registrar.messenger(), "flutter_package_manager");channel.setMethodCallHandler(new FlutterPackageManager(registrar));}private FlutterPackageManager(Registrar registrar) {this.registrar = registrar;}public void onMethodCall(MethodCall call, Result result) {if (call.method.equals("getPlatformVersion")) {result.success(android.os.Build.VERSION.RELEASE);} else if (call.method.equals("installThenStart")) {String path = call.arguments['filePath'];String activity = call.arguments['activity'];installApk(path, activity);} else {result.notImplemented();}}void installApk(String path, String activity) {// root权限静默安装实现 实现实际使用的是su pm install -r filePath命令。Process process = null; OutputStream out = null; InputStream in = null; try { // 请求root process = Runtime.getRuntime().exec("su"); out = process.getOutputStream(); // 调用安装 out.write(("pm install -r " + path + "\n").getBytes()); in = process.getInputStream(); int len = 0; byte[] bs = new byte[256]; while (-1 != (len = in.read(bs))) { String state = new String(bs, 0, len); if (state.equals("Success\n")) { //安装成功后的操作 startActivity(activity);} } } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) { out.flush(); out.close(); } if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } } }void startActivity(String activity) {// activity格式为'com.laileshuo.app/com.laileshuo.app.MainActivity'Intent mIntent = new Intent(); val componentName = ComponentName(this, activity)val intent = Intent().setComponent(componentName)startActivity(intent)}
}

当然,工程中的AndroidManifest.xml也需要做相应的调整,如下

<manifest xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"package="com.laileshuo.app"><applicationtools:replace="android:label"android:label="我的应用"android:name="${applicationName}"android:icon="@mipmap/ic_launcher"><activityandroid:name="com.laileshuo.app.MainActivity"android:exported="true"android:launchMode="singleTop"android:theme="@style/LaunchTheme"android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"android:hardwareAccelerated="true"android:windowSoftInputMode="adjustResize"><!-- Specifies an Android theme to apply to this Activity as soon asthe Android process has started. This theme is visible to the userwhile the Flutter UI initializes. After that, this theme continuesto determine the Window background behind the Flutter UI. --><meta-dataandroid:name="io.flutter.embedding.android.NormalTheme"android:resource="@style/NormalTheme"/><intent-filter><action android:name="android.intent.action.MAIN"/><category android:name="android.intent.category.LAUNCHER"/></intent-filter></activity><!-- Don't delete the meta-data below.This is used by the Flutter tool to generate GeneratedPluginRegistrant.java --><meta-dataandroid:name="flutterEmbedding"android:value="2" /></application>
</manifest>

2.2、如果非root环境安装,可以使用open_file插件

需要在pubspec.yaml引入插件

dependencies:open_file: ^3.3.2

在可以直接使用代码安装apk

import 'package:open_file/open_file.dart';OpenFile.open(apkFilePath);

当与关于FileProvider的其他插件发生冲突时,需要配置AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"package="xxx.xxx.xxxxx"><application>...<providerandroid:name="androidx.core.content.FileProvider"android:authorities="${applicationId}.fileProvider"android:exported="false"android:grantUriPermissions="true"tools:replace="android:authorities"><meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/filepaths"tools:replace="android:resource" /></provider></application>
</manifest>

三、小结

flutter开发实战-应用更新apk下载、安装apk、启动应用实现。在开发过程中,经常遇到需要更新下载新版本的apk文件,之后进行应用更新apk下载、安装apk、启动应用。我们在flutter工程中实现下载apk,判断当前版本与需要更新安装的版本进行比对判断,通过判断VersionCode来确定下载新版版APK。内容较多,描述可能不准确,请见谅。

https://blog.csdn.net/gloryFlow/article/details/133440529

学习记录,每天不停进步。

http://www.yayakq.cn/news/8624/

相关文章:

  • 做教育培训的网站闵行区网站开发
  • 千万pv网站开发成本中国建筑英才网官网手机版
  • 网页设计与网站开发超链接文交所网站开发
  • 简单html网站模板重庆旅游景点
  • 城市建设管理网站无锡自助网站
  • 长沙开发网站的公司哪家好住房和城乡建设部网站规范答疑
  • 网站买空间郑州妇科医院哪个医院最好
  • 淘宝客手机网站怎么做织梦网站推广插件
  • 东莞服务公司推广seo实战密码第四版pdf
  • 公司网站建设业务文案农村电子商务发展现状
  • 泰安网站建设报价百度指数查询
  • 网站开发 图片存放郑州生活信息网
  • 苏州高端网站建设设计网站建设哪里比较好
  • 创建一个网站的项目体现项目完成速度因素的wordpress 4.9.6 主题
  • 企业网站的建设意义番禺人才市场档案中心
  • 坪山网站建设代理商wordpress ico
  • 网站建设方案书 下载温州市建设工程质量监督站网站
  • 网站用什么颜色建设网站南沙区
  • 厦门制作企业网站网站备案认领
  • 上海自聊自做网站山东省技术标准创新中心
  • 广州建设官方网站wordpress侧边栏工具
  • 网站添加在线支付功能天府新区规划建设国土局网站
  • 做推广网站有什么屏蔽蜘蛛抓取 对网站有什么影响
  • 聊城集团网站建设价格视频网站开发的难点
  • 在建设厅网站上查询注销建造师wordpress反向代理 谷歌
  • 芜湖做网站建设公司wordpress域名防封插件
  • discuz修改网站标题做网站哪家好
  • 旅游网站建设风格apple官网登录入口
  • 青岛找网站建设公司哪家好涪陵网站建设
  • 沈阳制作网站企业您身边的网站建设专家