Skip to content

Commit 2450e6b

Browse files
author
Developer
committed
fix: handle REQUEST_INSTALL_PACKAGES permission on Android 8+
1 parent 9abc408 commit 2450e6b

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

hooks/useCheckUpdate.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,25 @@ export function useCheckUpdate() {
6060
}
6161
};
6262

63+
const openInstallSettings = () => {
64+
IntentLauncher.startActivityAsync(
65+
'android.settings.MANAGE_UNKNOWN_APP_SOURCES',
66+
{ data: 'package:com.anonymous.jkvideo' }
67+
).catch(() => {
68+
// 部分旧版 Android 不支持精确跳转,回退到通用安全设置
69+
IntentLauncher.startActivityAsync('android.settings.SECURITY_SETTINGS');
70+
});
71+
};
72+
73+
const triggerInstall = async (localUri: string) => {
74+
const contentUri = await FileSystem.getContentUriAsync(localUri);
75+
await IntentLauncher.startActivityAsync('android.intent.action.VIEW', {
76+
data: contentUri,
77+
flags: 1,
78+
type: 'application/vnd.android.package-archive',
79+
});
80+
};
81+
6382
const downloadAndInstall = async (url: string, version: string) => {
6483
if (Platform.OS !== 'android') {
6584
Alert.alert('提示', '自动安装仅支持 Android 设备');
@@ -83,12 +102,19 @@ export function useCheckUpdate() {
83102
await downloadResumable.downloadAsync();
84103
setDownloadProgress(null);
85104

86-
const contentUri = await FileSystem.getContentUriAsync(localUri);
87-
await IntentLauncher.startActivityAsync('android.intent.action.VIEW', {
88-
data: contentUri,
89-
flags: 1,
90-
type: 'application/vnd.android.package-archive',
91-
});
105+
try {
106+
await triggerInstall(localUri);
107+
} catch {
108+
// 无安装未知来源权限,引导用户开启后手动重试
109+
Alert.alert(
110+
'需要安装权限',
111+
'请在打开的设置页中,为 JKVideo 开启「允许安装未知应用」,然后回来再次点击「下载安装」。',
112+
[
113+
{ text: '取消', style: 'cancel' },
114+
{ text: '去设置', onPress: openInstallSettings },
115+
]
116+
);
117+
}
92118
} catch (e: any) {
93119
setDownloadProgress(null);
94120
Alert.alert('下载失败', e?.message ?? '请稍后重试');

0 commit comments

Comments
 (0)