处理openInstallPermissionSettings 问题
This commit is contained in:
parent
a666ebf74d
commit
b956113142
24
README.md
24
README.md
|
|
@ -255,6 +255,30 @@ final hasInstall = await PermissionHelper.checkAndRequestInstallPermission(
|
||||||
final hasNotification = await PermissionHelper.checkAndRequestNotificationPermission(
|
final hasNotification = await PermissionHelper.checkAndRequestNotificationPermission(
|
||||||
context: context,
|
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();
|
||||||
```
|
```
|
||||||
|
|
||||||
## 🐛 故障排除
|
## 🐛 故障排除
|
||||||
|
|
|
||||||
|
|
@ -103,6 +103,9 @@ class AppUpgradePlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
|
||||||
"getAndroidSdkVersion" -> {
|
"getAndroidSdkVersion" -> {
|
||||||
result.success(Build.VERSION.SDK_INT)
|
result.success(Build.VERSION.SDK_INT)
|
||||||
}
|
}
|
||||||
|
"openInstallPermissionSettings" -> {
|
||||||
|
openInstallPermissionSettings(result)
|
||||||
|
}
|
||||||
else -> {
|
else -> {
|
||||||
result.notImplemented()
|
result.notImplemented()
|
||||||
}
|
}
|
||||||
|
|
@ -191,6 +194,42 @@ class AppUpgradePlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
|
||||||
activity?.startActivity(intent)
|
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 {
|
private fun calculateMD5(file: File): String {
|
||||||
val digest = MessageDigest.getInstance("MD5")
|
val digest = MessageDigest.getInstance("MD5")
|
||||||
val inputStream = FileInputStream(file)
|
val inputStream = FileInputStream(file)
|
||||||
|
|
|
||||||
|
|
@ -70,6 +70,19 @@ class AppUpgradePlugin {
|
||||||
return AppUpgradePluginPlatform.instance.getAndroidSdkVersion();
|
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信息
|
/// 获取当前App信息
|
||||||
Future<Map<String, String>> getAppInfo() {
|
Future<Map<String, String>> getAppInfo() {
|
||||||
return AppUpgradePluginPlatform.instance.getAppInfo();
|
return AppUpgradePluginPlatform.instance.getAppInfo();
|
||||||
|
|
|
||||||
|
|
@ -213,6 +213,19 @@ class MethodChannelAppUpgradePlugin extends AppUpgradePluginPlatform {
|
||||||
return await methodChannel.invokeMethod<int>('getAndroidSdkVersion');
|
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
|
@override
|
||||||
Future<Map<String, String>> getAppInfo() async {
|
Future<Map<String, String>> getAppInfo() async {
|
||||||
final packageInfo = await PackageInfo.fromPlatform();
|
final packageInfo = await PackageInfo.fromPlatform();
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,11 @@ abstract class AppUpgradePluginPlatform extends PlatformInterface {
|
||||||
throw UnimplementedError('getAndroidSdkVersion() has not been implemented.');
|
throw UnimplementedError('getAndroidSdkVersion() has not been implemented.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 打开安装未知应用权限设置页面(仅Android)
|
||||||
|
Future<bool> openInstallPermissionSettings() {
|
||||||
|
throw UnimplementedError('openInstallPermissionSettings() has not been implemented.');
|
||||||
|
}
|
||||||
|
|
||||||
/// 配置HTTP设置
|
/// 配置HTTP设置
|
||||||
void configureHttp(HttpConfig config) {
|
void configureHttp(HttpConfig config) {
|
||||||
throw UnimplementedError('configureHttp() has not been implemented.');
|
throw UnimplementedError('configureHttp() has not been implemented.');
|
||||||
|
|
|
||||||
|
|
@ -195,7 +195,12 @@ class PermissionHelper {
|
||||||
);
|
);
|
||||||
|
|
||||||
if (openSettings) {
|
if (openSettings) {
|
||||||
await openAppSettings();
|
// 使用精确的安装权限设置页面
|
||||||
|
final opened = await _plugin.openInstallPermissionSettings();
|
||||||
|
if (!opened) {
|
||||||
|
// 如果无法打开精确页面,则使用通用应用设置页面作为后备
|
||||||
|
await openAppSettings();
|
||||||
|
}
|
||||||
// Give user time to change settings
|
// Give user time to change settings
|
||||||
await Future.delayed(const Duration(seconds: 1));
|
await Future.delayed(const Duration(seconds: 1));
|
||||||
}
|
}
|
||||||
|
|
@ -215,7 +220,12 @@ class PermissionHelper {
|
||||||
);
|
);
|
||||||
|
|
||||||
if (openSettings) {
|
if (openSettings) {
|
||||||
await openAppSettings();
|
// 使用精确的安装权限设置页面
|
||||||
|
final opened = await _plugin.openInstallPermissionSettings();
|
||||||
|
if (!opened) {
|
||||||
|
// 如果无法打开精确页面,则使用通用应用设置页面作为后备
|
||||||
|
await openAppSettings();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue