繁体   English   中英

如何打开APK文件并安装?

[英]How to open APK file and install?

我的代码:

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)但没有奏效。

您可以使用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

对于原生 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”的库

https://github.com/lucasferreira/react-native-send-intent#example--install-a-remote-apk

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM