flutter开发实战-安卓apk安装、卸载、启动实现

flutter开发实战-安卓apk安装、卸载、启动实现

在之前的文章中,实现了应用更新apk下载等操作,具体文档看下

这里记录一下使用shell来操作apk的安装、卸载、启动的操作。用到了库shell,Shell用于在Dart中或在代表其他用户执行系统管理任务的应用程序中编写shell实用程序脚本。

一、在pubspec.yaml中引入shell

在pubspec.yaml中引入shell

dependencies:
  shell: ^2.0.0

二、shell简介

Shell是dart:io〔Process〕API上的包装器以便支持环境管理、用户开关等功能。Shell用于在Dart中或在代表其他用户执行系统管理任务的应用程序中编写shell实用程序脚本。

下面是Shell的一个示例

import 'dart:io';
import 'package:file/local.dart';
import 'package:shell/shell.dart';

main() async {
  var fs = const LocalFileSystem();
  var shell = new Shell();
  var password = Platform.environment['PASSWORD'];
  print('Password: $password');

  // 将结果传输到文件。
  var echo = await shell.start('echo', ['hello world']);
  await echo.stdout.writeToFile(fs.file('hello.txt'));
  await echo.stderr.drain();

  //可以运行一个程序,并退出代码
  //如果返回了有效的退出代码,则会排出stderr,并且
  //您不必手动操作。
  //
  //否则,将引发StateError。
  var find = await shell.start('find', ['.']);
  await find.expectExitCode([0]); // Can also call find.expectZeroExit()

  // 转储输出.
  print(await find.stdout.readAsString());

  // 还可以运行一个进程并立即返回一个字符串。
  var pwd = await shell.startAndReadAsString('pwd', []);
  print('cwd: $pwd');

  // 可以导航到目录,类似 `cd`.
  shell.navigate('./lib/src');
  pwd = await shell.startAndReadAsString('pwd', []);
  print('cwd: $pwd');

  // 可以用相同的设置制作一个单独的外壳。
  var forked = new Shell.copy(shell)
    ..sudo = true
    ..password = password;

  // 输出echo
  var superEcho = await forked.start('echo', ['hello, admin!']);
  await superEcho.expectExitCode([0, 1]);
  await superEcho.stdout.writeToFile(fs.file('hello_sudo.txt'));
}

三、安卓apk安装、卸载、启动实现

使用Shell,我们需要shell程序脚本。那么安卓apk安装、卸载、启动实现,需要实现制定的Shell指令脚本。

在adb shell中,输入以下命令以安装APK

pm install /sdcard/Download/my_apk_release.apk

那么使用shell,则使用如下方法

  final _shell = Shell(runInShell: false);

  Future shellRun(List cmd) async {
    return await _shell.start('su', arguments: ['0', ...cmd]);
  }

  Future shellHandleResult(WrappedProcess result) async {
    var code = await result.exitCode;
    var s = await result.stdout.readAsString();
    if (code != 0) {
      var e = await result.stderr.readAsString();
      throw '$code:$e';
    }
  }

那直接调用安装Android安卓apk

可以实现install则使用安装apk

  @override
  Future install(String path) async {
    var result = await shellRun(['pm', 'install', '-r', path]);
    return shellHandleResult(result);
  }

卸载apk的uninstall

  @override
  Future uninstall(String packageName) async {
    var result = await shellRun(['pm', 'uninstall', '-k', packageName]);
    return shellHandleResult(result);
  }

启动安装的应用apk

  @override
  Future startApp(String componentName) async {
    var result = await shellRun(['am', 'start', '-n', componentName]);
    return shellHandleResult(result);
  }

强制停止指定的应用

  @override
  Future stopApp(String componentName) async {
    var result = await shellRun(['am', 'force-stop', '-n', componentName]);
    return shellHandleResult(result);
  }

应用安装并且启动

@override
  Future installThenStart(String path, String componentName) async {
    var result = await shellRun(['pm', 'install', '-r', path, '&&', 'am', 'start', '-n', componentName]);
    return shellHandleResult(result);
  }

至此,可以使用Sell来实现安卓apk安装、卸载、启动等操作。

四、小结

flutter开发实战-安卓apk安装、卸载、启动实现。描述可能不是特别准确,请见谅。

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

你可能感兴趣的:(移动开发,flutter开发实战,flutter,flutter,android,javascript,apk安装,apk,卸载,启动)