From b956113142a065e444ae7d9fbbb4921b562d9a91 Mon Sep 17 00:00:00 2001 From: "DESKTOP-I3JPKHK\\wy" <1111> Date: Wed, 17 Sep 2025 17:07:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86openInstallPermissionSettings?= =?UTF-8?q?=20=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 24 ++++++++++++ .../app_upgrade_plugin/AppUpgradePlugin.kt | 39 +++++++++++++++++++ lib/app_upgrade_plugin.dart | 13 +++++++ lib/app_upgrade_plugin_method_channel.dart | 13 +++++++ ...app_upgrade_plugin_platform_interface.dart | 5 +++ lib/core/permission_helper.dart | 14 ++++++- 6 files changed, 106 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 258a8ef..2b4a62e 100644 --- a/README.md +++ b/README.md @@ -255,6 +255,30 @@ final hasInstall = await PermissionHelper.checkAndRequestInstallPermission( final hasNotification = await PermissionHelper.checkAndRequestNotificationPermission( context: context, ); + +// 直接打开安装权限设置页面(新功能) +final opened = await AppUpgradePlugin().openInstallPermissionSettings(); +if (opened) { + print('成功打开安装权限设置页面'); +} else { + print('无法打开设置页面(非Android设备或系统限制)'); +} +``` + +#### 精确权限设置跳转 + +相比传统的 `openAppSettings()` 方法会打开应用的全部设置页面,新增的 `openInstallPermissionSettings()` 方法可以更精确地定位到安装权限设置: + +- **Android 8.0+**:直接跳转到"安装未知应用"权限页面 +- **Android 8.0-**:跳转到应用详情页面 +- **自动降级**:如果无法打开精确页面,会自动使用通用设置页面作为后备 + +```dart +// 使用新方法精确跳转到安装权限设置 +await AppUpgradePlugin().openInstallPermissionSettings(); + +// 对比:传统方法打开全部应用设置 +await openAppSettings(); ``` ## 🐛 故障排除 diff --git a/android/src/main/kotlin/com/example/app_upgrade_plugin/AppUpgradePlugin.kt b/android/src/main/kotlin/com/example/app_upgrade_plugin/AppUpgradePlugin.kt index ea1f6c4..cf65f72 100644 --- a/android/src/main/kotlin/com/example/app_upgrade_plugin/AppUpgradePlugin.kt +++ b/android/src/main/kotlin/com/example/app_upgrade_plugin/AppUpgradePlugin.kt @@ -103,6 +103,9 @@ class AppUpgradePlugin: FlutterPlugin, MethodCallHandler, ActivityAware { "getAndroidSdkVersion" -> { result.success(Build.VERSION.SDK_INT) } + "openInstallPermissionSettings" -> { + openInstallPermissionSettings(result) + } else -> { result.notImplemented() } @@ -191,6 +194,42 @@ class AppUpgradePlugin: FlutterPlugin, MethodCallHandler, ActivityAware { activity?.startActivity(intent) } + private fun openInstallPermissionSettings(result: Result) { + if (activity == null) { + result.error("NO_ACTIVITY", "Activity is not available", null) + return + } + + try { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + // Android 8.0 及以上版本,直接跳转到安装未知应用权限页面 + val intent = Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES) + intent.data = Uri.parse("package:${context.packageName}") + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + activity!!.startActivity(intent) + result.success(true) + } else { + // Android 8.0 以下版本,跳转到应用详情页面 + val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) + intent.data = Uri.parse("package:${context.packageName}") + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + activity!!.startActivity(intent) + result.success(true) + } + } catch (e: Exception) { + // 如果无法跳转到具体页面,则跳转到应用设置 + try { + val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) + intent.data = Uri.parse("package:${context.packageName}") + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + activity!!.startActivity(intent) + result.success(true) + } catch (fallbackException: Exception) { + result.error("SETTINGS_ERROR", "Failed to open install permission settings", fallbackException.message) + } + } + } + private fun calculateMD5(file: File): String { val digest = MessageDigest.getInstance("MD5") val inputStream = FileInputStream(file) diff --git a/lib/app_upgrade_plugin.dart b/lib/app_upgrade_plugin.dart index baabb4f..0e6b871 100644 --- a/lib/app_upgrade_plugin.dart +++ b/lib/app_upgrade_plugin.dart @@ -70,6 +70,19 @@ class AppUpgradePlugin { return AppUpgradePluginPlatform.instance.getAndroidSdkVersion(); } + /// 打开安装未知应用权限设置页面(仅Android) + /// + /// 对于Android 8.0及以上版本,会直接跳转到该应用的"安装未知应用"权限页面 + /// 对于Android 8.0以下版本,会跳转到应用详情页面 + /// + /// 返回true表示成功打开设置页面,false表示打开失败 + Future openInstallPermissionSettings() { + if (!Platform.isAndroid) { + return Future.value(false); + } + return AppUpgradePluginPlatform.instance.openInstallPermissionSettings(); + } + /// 获取当前App信息 Future> getAppInfo() { return AppUpgradePluginPlatform.instance.getAppInfo(); diff --git a/lib/app_upgrade_plugin_method_channel.dart b/lib/app_upgrade_plugin_method_channel.dart index 1668e25..22ab7c4 100644 --- a/lib/app_upgrade_plugin_method_channel.dart +++ b/lib/app_upgrade_plugin_method_channel.dart @@ -213,6 +213,19 @@ class MethodChannelAppUpgradePlugin extends AppUpgradePluginPlatform { return await methodChannel.invokeMethod('getAndroidSdkVersion'); } + @override + Future openInstallPermissionSettings() async { + if (!Platform.isAndroid) return false; + + try { + final result = await methodChannel.invokeMethod('openInstallPermissionSettings'); + return result ?? false; + } catch (e) { + debugPrint('Failed to open install permission settings: $e'); + return false; + } + } + @override Future> getAppInfo() async { final packageInfo = await PackageInfo.fromPlatform(); diff --git a/lib/app_upgrade_plugin_platform_interface.dart b/lib/app_upgrade_plugin_platform_interface.dart index 5441abd..826bad5 100644 --- a/lib/app_upgrade_plugin_platform_interface.dart +++ b/lib/app_upgrade_plugin_platform_interface.dart @@ -33,6 +33,11 @@ abstract class AppUpgradePluginPlatform extends PlatformInterface { throw UnimplementedError('getAndroidSdkVersion() has not been implemented.'); } + /// 打开安装未知应用权限设置页面(仅Android) + Future openInstallPermissionSettings() { + throw UnimplementedError('openInstallPermissionSettings() has not been implemented.'); + } + /// 配置HTTP设置 void configureHttp(HttpConfig config) { throw UnimplementedError('configureHttp() has not been implemented.'); diff --git a/lib/core/permission_helper.dart b/lib/core/permission_helper.dart index 727f8ef..2681653 100644 --- a/lib/core/permission_helper.dart +++ b/lib/core/permission_helper.dart @@ -195,7 +195,12 @@ class PermissionHelper { ); if (openSettings) { - await openAppSettings(); + // 使用精确的安装权限设置页面 + final opened = await _plugin.openInstallPermissionSettings(); + if (!opened) { + // 如果无法打开精确页面,则使用通用应用设置页面作为后备 + await openAppSettings(); + } // Give user time to change settings await Future.delayed(const Duration(seconds: 1)); } @@ -215,7 +220,12 @@ class PermissionHelper { ); if (openSettings) { - await openAppSettings(); + // 使用精确的安装权限设置页面 + final opened = await _plugin.openInstallPermissionSettings(); + if (!opened) { + // 如果无法打开精确页面,则使用通用应用设置页面作为后备 + await openAppSettings(); + } } }