处理openInstallPermissionSettings 问题

This commit is contained in:
DESKTOP-I3JPKHK\wy 2025-09-17 17:07:11 +08:00
parent a666ebf74d
commit b956113142
6 changed files with 106 additions and 2 deletions

View File

@ -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();
```
## 🐛 故障排除

View File

@ -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)

View File

@ -70,6 +70,19 @@ class AppUpgradePlugin {
return AppUpgradePluginPlatform.instance.getAndroidSdkVersion();
}
/// Android
///
/// Android 8.0"安装未知应用"
/// Android 8.0
///
/// true表示成功打开设置页面false表示打开失败
Future<bool> openInstallPermissionSettings() {
if (!Platform.isAndroid) {
return Future.value(false);
}
return AppUpgradePluginPlatform.instance.openInstallPermissionSettings();
}
/// App信息
Future<Map<String, String>> getAppInfo() {
return AppUpgradePluginPlatform.instance.getAppInfo();

View File

@ -213,6 +213,19 @@ class MethodChannelAppUpgradePlugin extends AppUpgradePluginPlatform {
return await methodChannel.invokeMethod<int>('getAndroidSdkVersion');
}
@override
Future<bool> openInstallPermissionSettings() async {
if (!Platform.isAndroid) return false;
try {
final result = await methodChannel.invokeMethod<bool>('openInstallPermissionSettings');
return result ?? false;
} catch (e) {
debugPrint('Failed to open install permission settings: $e');
return false;
}
}
@override
Future<Map<String, String>> getAppInfo() async {
final packageInfo = await PackageInfo.fromPlatform();

View File

@ -33,6 +33,11 @@ abstract class AppUpgradePluginPlatform extends PlatformInterface {
throw UnimplementedError('getAndroidSdkVersion() has not been implemented.');
}
/// Android
Future<bool> openInstallPermissionSettings() {
throw UnimplementedError('openInstallPermissionSettings() has not been implemented.');
}
/// HTTP设置
void configureHttp(HttpConfig config) {
throw UnimplementedError('configureHttp() has not been implemented.');

View File

@ -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();
}
}
}