Last active
July 18, 2019 12:26
-
-
Save nknr/b5452ea7f0d0deef9962c1b54bffba9a to your computer and use it in GitHub Desktop.
Android Download Manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String[] PERMISSIONS = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}; | |
private int RC_PERMISSIONS = 1; | |
private long downloadID; | |
private BroadcastReceiver onDownloadComplete = new BroadcastReceiver() { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
//Fetching the download id received with the broadcast | |
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); | |
//Checking if the received broadcast is for our enqueued download by matching download id | |
if (downloadID == id) { | |
Toast.makeText(MainActivity.this, "Download Completed", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
}; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
registerReceiver(onDownloadComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); | |
} | |
public void onDownload(View view) { | |
if (Permissions.hasPermissions(this, PERMISSIONS)) { | |
beginDownload(); | |
} else { | |
ActivityCompat.requestPermissions(this, PERMISSIONS, RC_PERMISSIONS); | |
} | |
} | |
@Override | |
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | |
super.onRequestPermissionsResult(requestCode, permissions, grantResults); | |
boolean valid = true; | |
if (requestCode == RC_PERMISSIONS && grantResults.length > 0) { | |
for (int result : grantResults) { | |
if (result == PackageManager.PERMISSION_GRANTED) | |
valid = true; | |
else { | |
super.onRequestPermissionsResult(requestCode, permissions, grantResults); | |
valid = false; | |
} | |
} | |
if (valid) { | |
beginDownload(); | |
} | |
} | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
unregisterReceiver(onDownloadComplete); | |
} | |
private void beginDownload() { | |
File file = new File(getExternalFilesDir(null), "Dummy"); | |
DownloadManager.Request request = new DownloadManager.Request(Uri.parse("http://speedtest.ftp.otenet.gr/files/test10Mb.db")) | |
// Title of the Download Notification | |
.setTitle("Dummy File") | |
// Description of the Download Notification | |
.setDescription("Downloading") | |
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI) | |
// Visibility of the download Notification | |
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE | DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) | |
// Uri of the destination file | |
.setDestinationUri(Uri.fromFile(file)) | |
// Set if download is allowed on roaming network | |
.setAllowedOverRoaming(true); | |
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); | |
if (downloadManager != null) { | |
downloadID = downloadManager.enqueue(request);// enqueue puts the download request in the queue. | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Permissions { | |
public static boolean hasPermissions(Context context, String... permissions) { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) { | |
for (String permission : permissions) { | |
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) { | |
return false; | |
} | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment