什么是Flutter Plugin
Flutter Plugin是一种特殊的包,包含一个用Dart编写的API定义,结合Android和iOS的平台特定实现,从而达到二者兼容。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13659.html
- 应用的Flutter部分通过平台通道(platform channel)将消息发送到其应用程序的所在的宿主(iOS或Android)
- 宿主监听的平台通道,并接收该消息。然后它会调用特定于该平台的API(使用原生编程语言) - 并将响应发送回客户端,即应用程序的Flutter部分 使用平台通道在客户端(Flutter UI)和宿主(平台)之间传递消息,如下图所示
创建Flutter App
相关代码见运行第一个Flutter App文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13659.html
创建Flutter Plugin
右键工程->New->Module,如下图所示文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13659.html
选择Flutter Plugin,点击Next,如下图所示文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13659.html
输入工程名(Project name),点击Next,如下图所示文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13659.html
输入包名(Package name),点击Finish,入下图所示文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13659.html
到此Flutter plugin创建完成。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13659.html
引入插件
在工程目录下找到pubspec.yaml
文件,在dev_dependencies
添加如下依赖,如下图所示文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13659.html
相关代码如下文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13659.html
dev_dependencies:
flutter_test:
sdk: flutter
install_apk_plugin:
path: install_apk_plugin
复制代码
获取版本号demo
打开插件lib下的dart文件,会有平台自动生成的代码,具体是实现获取APP版本号,如下面代码所示文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13659.html
class InstallApkPlugin {
static const MethodChannel _channel =
const MethodChannel('install_apk_plugin');
static Future<String> get platformVersion async {
final String version = await _channel.invokeMethod('getPlatformVersion');
return version;
}
}
复制代码
java部分的代码如下面所示文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13659.html
public class InstallApkPlugin implements MethodCallHandler {
private static final String TAG = "InstallApkPlugin";
private final Registrar registrar;
/**
* Plugin registration.
*/
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "install_apk_plugin");
channel.setMethodCallHandler(new InstallApkPlugin(registrar));
}
private InstallApkPlugin(Registrar registrar) {
this.registrar = registrar;
}
@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("getPlatformVersion")) {
result.success("Android " + android.os.Build.VERSION.RELEASE);
} else {
result.notImplemented();
}
}
}
复制代码
实现自动安装APK
实现自动安装APK,需要从Flutter应用层传入一个APK安装包的地址到host层,dart代码如下所示:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13659.html
class InstallApkPlugin {
static const MethodChannel _channel =
const MethodChannel('install_apk_plugin');
static Future<String> get platformVersion async {
final String version = await _channel.invokeMethod('getPlatformVersion');
return version;
}
static Future<bool> installApk(String path) async {
final bool isSuccess = await _channel.invokeMethod('installApk', path);
return isSuccess;
}
}
复制代码
java部分的代码如下所示文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13659.html
public class InstallApkPlugin implements MethodCallHandler {
private static final String TAG = "InstallApkPlugin";
private final Registrar registrar;
/**
* Plugin registration.
*/
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "install_apk_plugin");
channel.setMethodCallHandler(new InstallApkPlugin(registrar));
}
private InstallApkPlugin(Registrar registrar) {
this.registrar = registrar;
}
@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("getPlatformVersion")) {
result.success("Android " + android.os.Build.VERSION.RELEASE);
} else if (call.method.equals("installApk")) {
final String path = (String) call.arguments;
Log.d(TAG, "installApk path is " + path);
} else {
result.notImplemented();
}
}
}
复制代码
到此,host层就能获取到APK安装包的路径了,后面只需实现Android安装APK的代码逻辑即可,在日志下面添加如下代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13659.html
File file = new File(path);
installApk(file, registrar.context());
复制代码
installApk
代码实现如下所示文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13659.html
private void installApk(File apkFile, Context context) {
Intent installApkIntent = new Intent();
installApkIntent.setAction(Intent.ACTION_VIEW);
installApkIntent.addCategory(Intent.CATEGORY_DEFAULT);
installApkIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri apkUri = null;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
apkUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", apkFile);
installApkIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
apkUri = Uri.fromFile(apkFile);
}
installApkIntent.setDataAndType(apkUri, "application/vnd.android.package-archive");
if (context.getPackageManager().queryIntentActivities(installApkIntent, 0).size() > 0) {
context.startActivity(installApkIntent);
}
}
复制代码
除此之外,还需修改AndroidManifest.xml
内的代码,如下面代码所示文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13659.html
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.yuzo.install_apk_plugin">
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<application>
<!--provider start-->
<provider
android:name="androidx.core.content.FileProvider"
tools:replace="android:authorities"
android:authorities="com.yuzo.opengit.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
tools:replace="android:resource"
android:resource="@xml/file_path" />
</provider>
<!--provider end-->
</application>
</manifest>
复制代码
file_path.xml
放在res->xml文件夹下面,如下面代码所示文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13659.html
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:tools="http://schemas.android.com/tools"
tools:ignore="ResourceName">
<root-path
name="root_path"
path="." />
</paths>
复制代码
运行代码如下图所示文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13659.html
作者:Yuzo
链接:https://juejin.im/post/5d021b68e51d45777621bb65
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/13659.html