Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何打开APK文件并安装?_Android_React Native - Fatal编程技术网

Android 如何打开APK文件并安装?

Android 如何打开APK文件并安装?,android,react-native,Android,React Native,我的代码: import RNFS from 'react-native-fs'; const downloadDest = `${RNFS.CachesDirectoryPath}/${(Math.random() * 1000) | 0}.apk`; const options = { fromUrl: url, toFile: downloadDest, background: true, }; RNFS.downloadFile().promise.then(r

我的代码:

import RNFS from 'react-native-fs';

const downloadDest = `${RNFS.CachesDirectoryPath}/${(Math.random() * 1000) | 0}.apk`;
const options = {
    fromUrl: url,
    toFile: downloadDest,
    background: true,
};
RNFS.downloadFile().promise.then(res => {
  // How to open APK file and install?
})
如何打开并安装APK文件


我尝试过使用
Linking.openURL(downloadDest)
,但没有成功

对于本机android代码,您应该在下面发送安装类似APK的代码的意向

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "app.apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
您可以使用react native,这样就可以编写一个本机模块并公开API来调用。或者更方便地使用名为“react native send intent”的库


您可以使用
rn fetch blob
而不是
react native fs
。添加以下
RFFectchBlob.config
,APK下载完成后,它将自动为android安装

const android = RNFetchBlob.android;
RNFetchBlob.config({
      appendExt : 'apk',
      timeout:180000, 
      addAndroidDownloads : {
        notification : true,
        useDownloadManager : true,
        mime : 'application/vnd.android.package-archive',
        mediaScannable : true,
        title:"",
        description : '',
        path: "your apk download path"
    }
    })
      .fetch("GET", downloadUrl)
      .then(res => {
        if(res.respInfo.timeout){
          Linking.openURL(downloadUrl)
          return;
        }
        android.actionViewIntent(res.path(), 'application/vnd.android.package-archive')
      })
      .catch(error => {
        console.log(error);
        Linking.openURL(downloadUrl)
      });


或者使用hieuxit答案定义本机模块以打开APK

react native send intent
他是否有下载进度?不,react native send intent是一个库,用于从Android的react native code(js code)发送intent。它不是像rn fetch blob或react native fs那样的文件下载库
'android'没有定义。
android.actionViewIntent()
@ebyteebyte;我添加了它。@ebytebyte,如果你认为答案是好的并且对你有帮助,你也可以对答案进行投票。但奇怪的是,Android模拟器运行正常,但安装界面无法在Android手机上弹出(Android v8.1)