安装新版本后,Android应用已关闭

时间:2020-01-08 14:22:45

标签: android react-native

我有一个现有的react native应用程序,该应用程序添加了一些功能,并希望对其进行语法更新。我的问题是安装后我的应用程序无缘无故关闭,但应用程序正在安全更新。我该如何预防

我的build.gradle

ext {
        buildToolsVersion = "28.0.3"
        minSdkVersion = 16
        compileSdkVersion = 28
        targetSdkVersion = 28
        supportLibVersion = "28.0.0"
    }

我的本​​机下载部分

downloadClick = async () => {
        const { Model } = this.props;
        try {
            await requestPermission(Permissions.WRITE_EXTERNAL_STORAGE, trans('STORAGE_MESSAGE'));
            await requestPermission(Permissions.WRITE_EXTERNAL_STORAGE, trans('REQUEST_INSTALL_PACKAGES'));
            this.downloading(true);
            const filePath = `${RNFS.DocumentDirectoryPath}/com.rasapayam.apk`;
            RNFS.downloadFile({
                // fromUrl: Model.ApkLink.value,
                fromUrl: "https://srv-file7.gofile.io/download/2Oyk8V/rasa-60.apk",
                toFile: filePath,
                progress: (res) => {
                    this.setState({ progress: (res.bytesWritten / res.contentLength) });
                },
                progressDivider: 1,
            }).promise.then((result) => {
                this.downloading(false);
                if (result.statusCode === 200) {
                    MimeIntent.openURLWithMime(filePath, 'application/vnd.android.package-archive');
                }
            }).catch((err) => {
                this.downloading(false);
            });
        } catch (err) {
            ToastAndroid.show(trans('VERSION_ERROR'), ToastAndroid.SHORT);
            this.setState({ progress: 0 });
        }
    } 

我的Android本机模块,用于安装下载的APK

@ReactMethod
    public void openURLWithMime(String url, String mime, Promise promise) {
        if (url == null || url.isEmpty()) {
            promise.reject(new JSApplicationIllegalArgumentException("Invalid URL: " + url));
            return;
        }
        if (ContextCompat.checkSelfPermission(_context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            Activity currentActivity = getCurrentActivity();
            Uri contentUri = FileProvider.getUriForFile(_context, "com.rasapayam", new File( url));
            Intent install = new Intent(Intent.ACTION_VIEW);
            install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            install.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            install.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
            install.setData(contentUri);
            if (currentActivity != null) {
                currentActivity.startActivity(install);
            } else {
                getReactApplicationContext().startActivity(install);
            }
            promise.resolve(true);
        }
    }

我的清单的提供程序位于应用程序的标签上

 <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.rasapayam"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_provider_paths" />
        </provider>

我的提供商xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="external"
        path="." />
    <external-files-path name="external_files" path="." />
    <root-path name="root" path="." />
    <files-path
        name="files"
        path="." />
</paths>

2 个答案:

答案 0 :(得分:1)

我不是这里的专家,但是我非常有信心这是Android上的自然行为,当您安装新应用程序时,它需要关闭,Google Play会在更新时关闭该应用程序。您最有可能想要实现的是某种OTA更新。您可以使用Code Push AFAIK来实现。

答案 1 :(得分:0)

这是正常的规则。请记住,当您使用Android Studio时。当您在手机或仿真器中运行某个应用程序时,请通过更新进一步运行该应用程序,然后关闭应用程序界面,然后安装新的APK。这是默认过程。当您使用相同的软件包名称安装应用程序时,它将替换先前的版本。这需要停止当前进程。