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

个人网页设计说明500字seo工具下载

个人网页设计说明500字,seo工具下载,购物网站及app开发,学校网站建设开发方案书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.hkea.cn/news/565498/

相关文章:

  • 做公司网站要注意哪些问题真正免费建站网站
  • 在线服务器代理杭州seo网络公司
  • wordpress邮件订阅seo技术外包
  • 深圳营销网站建站公司搜索引擎关键词的工具
  • 做网站如何网站考虑优化游戏推广员是诈骗吗
  • 公众号做视频网站吗关键词排名怎么做上首页
  • 重庆做网站价格优化软件下载
  • 如何做网站镜像今日最火的新闻
  • 水果网站开发所需的成本市场营销实际案例
  • 无锡市新吴区住房和建设交通局网站西安百度关键词包年
  • 网站平台方案设计seo上首页
  • 郑州做网站的联系方式搜狗友链交换
  • 一般建设一个网站多少钱怎么接广告赚钱
  • 计算机专业网站开发方向销售推广方案
  • 上海网站建设公司排名西安百度公司
  • 中国网网址是多少网站推广优化教程
  • 关于加强机关网站建设运营培训
  • dw做的网站怎么让别人看到如何建立一个网站
  • 保险网站建设优缺点seo代码优化步骤
  • 如何快速建网站百度电脑版入口
  • 山东省建设工程信息网站最近最新的新闻
  • 免费网站建设方案锦绣大地seo官网
  • 电子商务的网站建设牛排seo系统
  • 资源收费网站怎么做网站快速优化排名官网
  • 招标网哪个网站信息可靠百度站长工具网站
  • 郑州七七网站建设互联网推广公司
  • 佛山做外贸网站代理商百度收录技术
  • 公司网站建设需要什么今日热搜第一名
  • 烟台建设企业网站网站快速收录入口
  • 怎么做繁体字网站网络营销公司注册找哪家