以编程方式安装Android App中的问题(AutoUpdate)

问题描述 投票:-1回答:1

自动更新中的问题。如果有可用更新,则下载新应用并卸载当前应用并安装新应用。这一切都运行良好,直到目标SDK 22.但目标SDK 24或更新电话操作系统以上API级别2此功能不起作用每当我安装它显示包解析错误。因为旧的应用程序已经存在,我们正在尝试安装新的应用程序。

targetSDK:24

compileSDK:28

minSDK:19

这是一段代码:

public class UpdateActivity extends AppCompatActivity implements AppConstants {

    public static final String TAG = UpdateActivity.class.getSimpleName();
    private String appName = "XYZ.apk";
    private final Handler mHideHandler = new Handler();
    private View mContentView;
    private TextView txtDownloading;
    private NumberProgressBar numberProgressBar;
    private String updateUrl = "";

    private final Runnable mHidePart2Runnable = new Runnable() {
        @SuppressLint("InlinedApi")
        @Override
        public void run() {
            // Delayed removal of status and navigation bar

            mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);
        updateUrl = getIntent().getStringExtra("url");
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build());
        mContentView = findViewById(R.id.fullscreen_content);
        mHideHandler.post(mHidePart2Runnable);
        numberProgressBar = (NumberProgressBar) findViewById(R.id.numberbar8);
        txtDownloading = (TextView) findViewById(R.id.txtDownload);
        try {
            new DownloadFileFromURL().execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class DownloadFileFromURL extends AsyncTask<String, Integer, String> {

        DownloadFileFromURL() {
        }

        protected void onPreExecute() {
            super.onPreExecute();
            }

        protected String doInBackground(String... f_url) {
            try {
                HttpURLConnection c = (HttpURLConnection) new URL(updateUrl).openConnection();
                c.setRequestMethod("POST");
                c.setDoOutput(false);
                c.connect();
                int lengthOfFile = c.getContentLength();
                File file = new File(Environment.getExternalStorageDirectory().getPath() + "/Download/");
                file.mkdirs();
                File outputFile = new File(file, appName);
                if (outputFile.exists()) {
                    outputFile.delete();
                }
                FileOutputStream fos = new FileOutputStream(outputFile);
                InputStream is = c.getInputStream();
                byte[] data = new byte[AccessibilityNodeInfoCompat.ACTION_NEXT_HTML_ELEMENT];
                long total1 = 0;
                while (true) {
                    int count = is.read(data);
                    if (count == -1) {
                        break;
                    }
                    total1 += (long) count;
                    Integer[] strArr = new Integer[1];
                    strArr[0] = DOWNLOAD_COUNT + ((int) ((100 * total1) / ((long) lengthOfFile)));
                    publishProgress(strArr);
                    fos.write(data, 0, count);
                }
                fos.close();
                is.close();
                installApp();

            } catch (Exception e) {
                Log.e("Error: ", e.getMessage());
            }
            return null;
        }

        protected void onProgressUpdate(Integer... progress) {
            try {
                numberProgressBar.setUnreachedBarColor(Color.parseColor("#CCCCCC"));
                numberProgressBar.setProgressTextSize(30.0f);
                if (progress[0] > 0 && progress[0] <= 20) {
                    numberProgressBar.setProgressTextColor(Color.parseColor("#E74C3C"));
                    numberProgressBar.setReachedBarColor(Color.parseColor("#E74C3C"));
                    txtDownloading.setTextColor(Color.parseColor("#E74C3C"));
                }
                if (progress[0] > 20 && progress[0] <= 40) {
                    numberProgressBar.setProgressTextColor(Color.parseColor("#FF3D7F"));
                    numberProgressBar.setReachedBarColor(Color.parseColor("#FF3D7F"));
                    txtDownloading.setTextColor(Color.parseColor("#FF3D7F"));
                }
                if (progress[0] > 40 && progress[0] <= 60) {
                    numberProgressBar.setProgressTextColor(Color.parseColor("#FFC73B"));
                    numberProgressBar.setReachedBarColor(Color.parseColor("#FFC73B"));
                    txtDownloading.setTextColor(Color.parseColor("#FFC73B"));
                }
                if (progress[0] > 60 && progress[0] <= 80) {
                    numberProgressBar.setProgressTextColor(Color.parseColor("#6DBCDB"));
                    numberProgressBar.setReachedBarColor(Color.parseColor("#6DBCDB"));
                    txtDownloading.setTextColor(Color.parseColor("#6DBCDB"));
                }
                if (progress[0] > 80 && progress[0] <= 100) {
                    numberProgressBar.setProgressTextColor(Color.parseColor("#70A800"));
                    numberProgressBar.setReachedBarColor(Color.parseColor("#70A800"));
                    txtDownloading.setTextColor(Color.parseColor("#70A800"));
                }
                numberProgressBar.setProgress(progress[0]);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        protected void onPostExecute(String file_url) {
            numberProgressBar.setVisibility(View.GONE);

        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void installApp() {
        try {
            File apkFile = new File(Environment.getExternalStorageDirectory().getPath() + "/Download/", appName);
            Intent intent = new Intent("android.intent.action.VIEW");
            intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            finish();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    }
java android apk android-install-apk
1个回答
0
投票

我遇到了同样的问题,并使用以下方法解决了这个问题:

  • 从Playstore应用程序禁用Google Play Protect起了作用,即它阻止了外部源的安装,我能够摆脱解析错误。
  • 在Android N及以上版本中,为了访问特定文件或文件夹以使其可供其他应用程序访问,您无法使用File Uri,而是必须使用来自FileProvider类的Provider Uri,否则我们将获得FileUriExposedException,如android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()等。

有关如何设置FileProvider的更多信息,请参阅本指南:https://developer.android.com/reference/android/support/v4/content/FileProvider

希望这可以帮助。

© www.soinside.com 2019 - 2024. All rights reserved.