Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.downmanager;
- import android.support.v7.app.ActionBarActivity;
- import android.app.DownloadManager;
- import android.app.DownloadManager.Query;
- import android.app.DownloadManager.Request;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.database.Cursor;
- //import android.app.DownloadManager.Request;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Environment;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.TextView;
- public class MainActivity extends ActionBarActivity {
- DownloadManager dManager;
- TextView tvMessage;
- EditText urlInput;
- long did;
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // Get DownloadManager instance
- dManager=(DownloadManager)this.getSystemService(Context.DOWNLOAD_SERVICE);
- tvMessage=(TextView)findViewById(R.id.txtmessage);
- urlInput=(EditText)findViewById(R.id.txturl);
- }
- public void downloadFile(View view){
- String urlString=urlInput.getText().toString();
- if(!urlString.equals("")){
- try{
- // Get file name from the url
- String fileName=urlString.substring(urlString.lastIndexOf("/")+1);
- // Create Download Request object
- DownloadManager.Request request=new DownloadManager.Request(Uri.parse((urlString)));
- // Display download progress and status message in notification bar
- request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
- // Set description to display in notification
- request.setDescription("Download "+fileName+" from "+ urlString);
- // Set title
- request.setTitle("DownloadManager");
- // Set destination location for the downloaded file
- request.setDestinationUri(Uri.parse("file://"+Environment.getExternalStorageDirectory()+"/"+fileName));
- // Download the file if the Download manager is ready
- did=dManager.enqueue(request);
- }catch(Exception e){}
- }
- }
- // BroadcastReceiver to receive intent broadcast by DownloadManager
- private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context arg0, Intent arg1) {
- // TODO Auto-generated method stub
- Query q = new Query();
- q.setFilterById(did);
- Cursor cursor = dManager.query(q);
- if(cursor.moveToFirst()){
- String message="";
- int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
- if(status==DownloadManager.STATUS_SUCCESSFUL){
- message="Download successful";
- }
- else if(status==DownloadManager.STATUS_FAILED){
- message="Download failed";
- }
- tvMessage.setText(message);
- }
- }
- };
- protected void onResume(){
- super.onResume();
- // Register the receiver to receive an intent when download complete
- IntentFilter intentFilter= new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
- registerReceiver(downloadReceiver, intentFilter);
- }
- @Override
- protected void onPause() {
- // TODO Auto-generated method stub
- super.onPause();
- // Unregister the receiver
- unregisterReceiver(downloadReceiver);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement