Advertisement
pawelnowacki

Untitled

Jun 23rd, 2016
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. package com.example.downmanager;
  2.  
  3.  import android.support.v7.app.ActionBarActivity;
  4.  import android.app.DownloadManager;
  5.  import android.app.DownloadManager.Query;
  6.  import android.app.DownloadManager.Request;
  7.  import android.content.BroadcastReceiver;
  8.  import android.content.Context;
  9.  import android.content.Intent;
  10.  import android.content.IntentFilter;
  11.  import android.database.Cursor;
  12.  //import android.app.DownloadManager.Request;
  13.  import android.net.Uri;
  14.  import android.os.Bundle;
  15.  import android.os.Environment;
  16.  import android.view.Menu;
  17.  import android.view.MenuItem;
  18.  import android.view.View;
  19.  import android.widget.EditText;
  20.  import android.widget.TextView;
  21.  
  22.  public class MainActivity extends ActionBarActivity {
  23.  
  24.     DownloadManager dManager;
  25.     TextView tvMessage;
  26.     EditText urlInput;
  27.     long did;
  28.     protected void onCreate(Bundle savedInstanceState) {
  29.         super.onCreate(savedInstanceState);
  30.         setContentView(R.layout.activity_main);
  31.         // Get DownloadManager instance
  32.         dManager=(DownloadManager)this.getSystemService(Context.DOWNLOAD_SERVICE);
  33.         tvMessage=(TextView)findViewById(R.id.txtmessage); 
  34.         urlInput=(EditText)findViewById(R.id.txturl);
  35.     }
  36.    
  37.     public void downloadFile(View view){
  38.        
  39.         String urlString=urlInput.getText().toString();
  40.         if(!urlString.equals("")){
  41.             try{
  42.                 // Get file name from the url
  43.                 String fileName=urlString.substring(urlString.lastIndexOf("/")+1);
  44.                 // Create Download Request object
  45.                 DownloadManager.Request request=new DownloadManager.Request(Uri.parse((urlString)));
  46.                 // Display download progress and status message in notification bar
  47.                 request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);            
  48.                 // Set description to display in notification
  49.                 request.setDescription("Download "+fileName+" from "+ urlString);
  50.                 // Set title
  51.                 request.setTitle("DownloadManager");
  52.                 // Set destination location for the downloaded file
  53.                 request.setDestinationUri(Uri.parse("file://"+Environment.getExternalStorageDirectory()+"/"+fileName));
  54.                 // Download the file if the Download manager is ready
  55.                 did=dManager.enqueue(request);
  56.                
  57.             }catch(Exception e){}
  58.            
  59.         }
  60.        
  61.    
  62.     }
  63.     // BroadcastReceiver to receive intent broadcast by DownloadManager
  64.     private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
  65.  
  66.          @Override
  67.          public void onReceive(Context arg0, Intent arg1) {
  68.           // TODO Auto-generated method stub
  69.              Query q = new Query();
  70.              q.setFilterById(did);
  71.              Cursor cursor = dManager.query(q);
  72.              if(cursor.moveToFirst()){
  73.                  String message="";
  74.                   int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
  75.                   if(status==DownloadManager.STATUS_SUCCESSFUL){
  76.                       message="Download successful";
  77.                     }
  78.                   else if(status==DownloadManager.STATUS_FAILED){
  79.                       message="Download failed";
  80.                         }
  81.                   tvMessage.setText(message);
  82.                     }    
  83.              
  84.                
  85.                 }
  86.         };
  87.     protected void onResume(){
  88.         super.onResume();
  89.         // Register the receiver to receive an intent when download complete
  90.         IntentFilter intentFilter= new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
  91.         registerReceiver(downloadReceiver, intentFilter);
  92.     }
  93.     @Override
  94.     protected void onPause() {
  95.      // TODO Auto-generated method stub
  96.      super.onPause();
  97.      // Unregister the receiver
  98.      unregisterReceiver(downloadReceiver);
  99.     }
  100.    
  101.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement