0

我试图让用户能够从设备更新应用程序。我们有一个服务器,用户可以在其中下载带有新版本应用程序的 apk 文件。这是按下按钮后开始的代码:

Handler handler = new Handler(Looper.getMainLooper());
                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        File folder = new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString());
                        File file = new File(folder.getAbsolutePath(), "localdb.apk");
                        final Uri uri = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) ?
                                FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".provider", file) :
                                Uri.fromFile(file);

                        if (file.exists())
                            file.delete();

                        //url to get app from server
                        String url = Names.UPDATE_URL;

                        InputStream input = null;
                        OutputStream output = null;
                        HttpURLConnection connection = null;
                        try {
                            URL sUrl = new URL(url);
                            connection = (HttpURLConnection) sUrl.openConnection();
                            connection.connect();

                            // download the file
                            input = connection.getInputStream();
                            output = new FileOutputStream(file);

                            byte data[] = new byte[4096];
                            int count;
                            while ((count = input.read(data)) != -1) {
                                // allow canceling with back button

                                output.write(data, 0, count);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                if (output != null)
                                    output.close();
                                if (input != null)
                                    input.close();
                            } catch (IOException ignored) {
                            }

                            if (connection != null)
                                connection.disconnect();
                        }
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                Intent install = new Intent(Intent.ACTION_INSTALL_PACKAGE)
                                        .setData(uri)
                                        .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

                                startActivity(install);
                            }
                        });

                    }
                });
                thread.start();

这是我在清单中的内容:

...<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.QUICKBOOT_POWERON" />
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
...
 <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="ru.mob.myapp.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>
...

最后是一个xml文件provider_paths:

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

所以,一切正常,我下载了新版本的 apk,它与已安装的名称相同,系统要求我安装(更新)我的应用程序,但是当一切都完成时,我收到“应用程序未安装”消息没有任何详细信息. 有没有人有解决方案,提示,建议?

4

0 回答 0