flutter android11 获取未知应用权限封装

发布于 2024-10-21  98 次阅读


思路:通过通信临时获取安装权限,不走获取应用安装权限(原生通信请看通信封装)

1.安装方法(原生)

         fun getInstalledApps(path: String,packageName:String):Boolean {
            var isGetInstallApp:Boolean = false;
            val intent = Intent(Intent.ACTION_INSTALL_PACKAGE);
            val authority = "com.ucang.sfive.provider";
            val file = File(path)
            intent.setDataAndType(FileProvider.getUriForFile(this,"com.ucang.sfive.provider", file), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivity(intent);
            isGetInstallApp = true;
            return isGetInstallApp;
        }

2.通道调取

             if(call.method == "getInstalledApps"){
               val path:String = call.argument<String>("path")!!
               val packageName:String = call.argument<String>("packageName")!!
               if(path != null){
                val isGetInstall = getInstalledApps(path,packageName)
                if(isGetInstall){
                    result.success(isGetInstall)
                }else {
                    result.error("UNAVAILABLE", "installAppsPermisson is not get.", null)
                }
               }else{
                   result.error("UNAVAILABLE", "path is not support.", null)
               }

3.flutter 安装

//安装 save 为安装包路径  
Future<void> _getInstalledApps(String savePath) async {
    try {
      // String path = await _localPath;
      if (savePath != '') {
        final result = await setPermissions.invokeNativeMethod(
            'getInstalledApps',
            {'path': savePath, 'packageName': 'com.example.networktest1'});
        print(result['result']);
      } else {
        print('安装路径获取失败');
      }
    } on PlatformException catch (e) {
      print(e.message);
      // batteryLevel = "Failed to get battery level: '${e.message}'.";
    }
  }

4.权限配置

....略

一个练习时长还差一年半的切图仔,有需求可联系 QQ:963827384
最后更新于 2024-10-21