Flutter Apk 自动安装插件实现(二)

关于 AndroidX 的适配,以添加 Apk 自动安装功能为例

在 gradle.properties 中添加:

android.useAndroidX=true

android.enableJetifier=true

在 build.gradle 中添加:

//FileProvider
implementation 'androidx.appcompat:appcompat:1.0.2'

在 android 节点下添加:

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

选用 Java8 编译。

安装 APK 功能实现

FlutterAndroidPlugin

    //来自Flutter的方法调用
    @Override
    public void onMethodCall(MethodCall call, Result result) {

        String target = call.method;
        switch (target) {
            case "getPlatformVersion":
                result.success("Android " + android.os.Build.VERSION.RELEASE);
                break;
            case "toast":
                String content = (String) call.arguments;
                Log.d(TAG, "toast: " + content);
                showToast(content);
                break;
            case "installApk":
                String path = (String) call.arguments;
                Log.d(TAG, "install" + path);
                File file = new File(path);
                installApk(file, registrar.context());
                break;
            default:
                result.notImplemented();
                break;
        }

    }

        /**
     * 安装APK
     *
     * @param apk
     * @param context
     */
    private void installApk(File apk, Context context) {
        Intent installApkIntent = new Intent();
        installApkIntent.setAction(Intent.ACTION_VIEW);
        installApkIntent.addCategory(Intent.CATEGORY_DEFAULT);
        installApkIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        Uri uri = null;
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            uri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", apk);
            installApkIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            uri = Uri.fromFile(apk);
        }
        installApkIntent.setDataAndType(uri, "application/vnd.android.package-archive");

        if (context.getPackageManager().queryIntentActivities(installApkIntent, 0).size() > 0) {
            context.startActivity(installApkIntent);
        }

    }

AndroidManifest.xml

在清单配置文件中添加:

    

完整如下:



    
    
        
        
            
        
        

    



  • authorities:你 app 的包名.fileProvider
  • grantUriPermissions:必须是 true,表示授予 URI 临时访问权限
  • exported:必须是 false
  • resource:中的@xml/paths 是我们接下来要添加的文件
paths


    

  • files-path 对应 Context.getFilesDir()
  • cache-path 对应 getCacheDir()
  • external-path 对应 Environment.getExternalStorageDirectory()
  • external-files-path 对应 Context.getExternalFilesDir(String) Context.getExternalFilesDir(null)
  • external-cache-path 对应 Context.getExternalCacheDir()
flutter_plugin.dart

 /**
   * 安装Apk
   */
  static Future installApk(String path) async{
    return await _channel.invokeMethod("installApk",path);
  }

效果

Flutter Apk 自动安装插件实现(二)_第1张图片
Flutter Apk 自动安装插件实现(二)_第2张图片
Flutter Apk 自动安装插件实现(二)_第3张图片

最后

源码地址
贴一张自己学习Flutter的公众号,感兴趣的小伙伴可以一起学习哦。。。

Flutter Apk 自动安装插件实现(二)_第4张图片

你可能感兴趣的:(Flutter Apk 自动安装插件实现(二))