-1

An issue in auto-updating.That downloads a new app if an update is available and uninstall a current application and install a new app. It all works well till target SDK 22. but After target SDK 24 or updating Phone os above API Level 2 this functionality not working Whenever I install it shows package parsing error. because of an old app is already there and we are trying to install a new app.

targetSDK:24

compileSDK:28

minSDK:19

Here is a piece of code:

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();
        }
    }
    }
Rahul Chokshi
  • 670
  • 4
  • 18

1 Answers1

0

I faced the same issue, and resolved it using the following:

  • Disabling Google Play Protect from playstore app worked the trick, i-e it was blocking the installation from external sources and i was able to get rid of the parse error.

  • On Android N and above, to give access to particular file or folder to make them accessible for other apps, you cant use File Uri, instead you have to use Provider Uri from FileProvider class , otherwise we get the FileUriExposedException such as android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData() etc.

For more information to how to setup FileProvider, see this guide: https://developer.android.com/reference/android/support/v4/content/FileProvider

Hope this helps.

Qasim
  • 5,181
  • 4
  • 30
  • 51