title: Issue in Android App installing programmatically(AutoUpdate)
body: <p>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.</p>
  
  <p><strong>targetSDK:24</strong></p>
  
  <p><strong>compileSDK:28</strong></p>
  
  <p><strong>minSDK:19</strong></p>
  
  <p>Here is a piece of code:</p>
  
  <pre><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&lt;String, Integer, String&gt; {
  
          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] &gt; 0 &amp;&amp; progress[0] &lt;= 20) {
                      numberProgressBar.setProgressTextColor(Color.parseColor("#E74C3C"));
                      numberProgressBar.setReachedBarColor(Color.parseColor("#E74C3C"));
                      txtDownloading.setTextColor(Color.parseColor("#E74C3C"));
                  }
                  if (progress[0] &gt; 20 &amp;&amp; progress[0] &lt;= 40) {
                      numberProgressBar.setProgressTextColor(Color.parseColor("#FF3D7F"));
                      numberProgressBar.setReachedBarColor(Color.parseColor("#FF3D7F"));
                      txtDownloading.setTextColor(Color.parseColor("#FF3D7F"));
                  }
                  if (progress[0] &gt; 40 &amp;&amp; progress[0] &lt;= 60) {
                      numberProgressBar.setProgressTextColor(Color.parseColor("#FFC73B"));
                      numberProgressBar.setReachedBarColor(Color.parseColor("#FFC73B"));
                      txtDownloading.setTextColor(Color.parseColor("#FFC73B"));
                  }
                  if (progress[0] &gt; 60 &amp;&amp; progress[0] &lt;= 80) {
                      numberProgressBar.setProgressTextColor(Color.parseColor("#6DBCDB"));
                      numberProgressBar.setReachedBarColor(Color.parseColor("#6DBCDB"));
                      txtDownloading.setTextColor(Color.parseColor("#6DBCDB"));
                  }
                  if (progress[0] &gt; 80 &amp;&amp; progress[0] &lt;= 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();
          }
      }
      }
  </code></pre>