title: Android DownloadManager - Download apk and install it
body: <p>I have looked everywhere to find out how to download a APK that is not from the market place. The APK is stored in my server. 
  All the sample did not work.</p>
  
  <p>I want my App to download an APK for my server and promt the user to install it.</p>
  
  <p>So far I managed to download the apk onto the user sd. but I can't get he path where is it saved and get to install it.</p>
  
  <pre><code>private void downloadAPK() {
      String url="https://www.example.com/app.apk”;
      downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
      request = new Request(Uri.parse(url));
      request.setTitle(“My App“);
      enqueue = downloadManager.enqueue(request);
  
  
        new Thread(new Runnable() {
  
          @Override
          public void run() {
  
              boolean downloading = true;
  
              while (downloading) {
  
                  try {
                      Thread.sleep(10000);
                       DownloadManager.Query q = new DownloadManager.Query();
                          q.setFilterById(enqueue);
  
                          Cursor cursor = downloadManager.query(q);
                          cursor.moveToFirst();
                          int bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                          int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
  
                          if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
                              downloading = false;
                          }
  
                          final int dl_progress = (int) ((double)bytes_downloaded / (double)bytes_total * 100f);
  
                          runOnUiThread(new Runnable() {
  
                              @Override
                              public void run() {
  
                                  Log.d("Prog", Integer.toString(dl_progress));
                              }
                          });
  
  
                          cursor.close();
                  } catch (InterruptedException e) {
  
                  }
  
              }
  
          }
      }).start();
  }}
  
  
  BroadcastReceiver receiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();
  
  
          if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
               Log.w(“”, “Downloaded”)
  
          }
          else {
              //do something here
         }
  
      }
  };
  
  
  onCreate ...
  registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
  </code></pre>