rn-fetch-blob actionViewIntent() 不适用于 Android 8.1 手机? [英] rn-fetch-blob actionViewIntent() Does not work on Android 8.1 phones?

查看:98
本文介绍了rn-fetch-blob actionViewIntent() 不适用于 Android 8.1 手机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import RNFetchBlob from 'rn-fetch-blob';

      RNFetchBlob.android
        .actionViewIntent(
          '/storage/emulated/0/Android/data/aaa.bbb.ccc/files/184.apk',
          'application/vnd.android.package-archive',
        )
        .then(() => {
          console.log('success');
        })
        .catch(err => {
          console.log('error');
        });

在模拟器中,上述代码正常运行,并弹出安装APK界面

In the simulator, the above code works normally, and pops up the installation APK interface

在手机上(Android 8.1),屏幕闪白,然后什么也没有,也无法弹出安装界面,没有错误提示

On the phone (Android 8.1), the screen flashed white, then there was nothing, and the installation interface could not pop up, No error message

推荐答案

你应该在 AndroidMainFest.xml 中添加权限:

you should add permission in the AndroidMainFest.xml:

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

但由于是动态权限,建议您每次下载安装APK前检查是否有权限.

but because it is dynamic permission, I suggest you every time before you download and install APK to check it if it has permission.

在这种情况下,你可以写一个原生模块,动态检查权限你可以看到这个answer 进入权限集页面后

In this situation, you can write a native module, for dynamic check permission you can see this answer after it goes to the permission set page

您还可以使用 react-native-permission 库.

requestInstallUpdate = async () =>  {
  try {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.REQUEST_INSTALL_PACKAGES,
      {
        'title': 'Test App',
        'message': 'Test App needs to install updates.'
      }
    )
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      console.log("You can now install updates")
      return true;
    } else {
      console.log("App install permission denied")
      return false;
    }
  } catch (err) {
    console.warn(err)
    return false;
  }
}

然后在您的下载方法中

let canInstall = await requestInstallUpdate() 
if(canInstall){
  //download apk
}

这篇关于rn-fetch-blob actionViewIntent() 不适用于 Android 8.1 手机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆