Advertisement
Guest User

MainActivity

a guest
Aug 4th, 2015
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1.     public class MainActivity extends ActionBarActivity {
  2.     String myHTTPUrl = "http://infodeveloper.altervista.org/mAh/current/mAh.apk";
  3.  
  4.     @Override
  5.     protected void onCreate(Bundle savedInstanceState) {
  6.         super.onCreate(savedInstanceState);
  7.         setContentView(R.layout.activity_main);
  8.  
  9.         Button btnTrad, btnDM;
  10.         btnTrad = (Button)findViewById(R.id.btnTrad);
  11.  
  12.  
  13.  
  14.         btnTrad.setOnClickListener(new View.OnClickListener() {
  15.             @Override
  16.             public void onClick(View v) {
  17.  
  18.                 new MyTask().execute();
  19.                
  20.             }
  21.         });}
  22.     public class MyTask extends AsyncTask<Void, Void, Void>{
  23.        
  24.         @Override
  25.         protected Void doInBackground(Void... params) {
  26.  
  27.             try {
  28.                
  29.                 URL myurl = new URL (myHTTPUrl);
  30.                 HttpURLConnection connection = (HttpURLConnection)myurl.openConnection();
  31.                 connection.setDoOutput(true);
  32.                 connection.setRequestMethod("GET");
  33.                 connection.connect();
  34.  
  35.                 File rootDirectory = new File (Environment.getExternalStoragePublicDirectory(
  36.                         Environment.DIRECTORY_DOWNLOADS), "MieiDownload");
  37.                 if (!rootDirectory.exists()) {
  38.                      rootDirectory.mkdirs();
  39.                 }
  40.                 String nameOfFile = URLUtil.guessFileName(myHTTPUrl, null,
  41.                         MimeTypeMap.getFileExtensionFromUrl(myHTTPUrl));
  42.                 File file = new File (rootDirectory, nameOfFile);
  43.                 file.createNewFile();
  44.  
  45.  
  46.  
  47.                 InputStream  inputStream = connection.getInputStream();
  48.                 FileOutputStream output = new FileOutputStream(file);
  49.                
  50.                 byte[] buffer = new byte [1024];
  51.                 int byteCount = 0;
  52.                
  53.                 while((byteCount = inputStream.read(buffer))> 0){
  54.                     output.write(buffer, 0, byteCount);
  55.  
  56.                 }
  57.                 output.close();
  58.  
  59.                 Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
  60.                 intent.setData(Uri.fromFile(file));
  61.                 getApplicationContext().sendBroadcast(intent);
  62.  
  63.             } catch (MalformedURLException e) {
  64.  
  65.                 e.printStackTrace();
  66.             } catch (IOException e) {
  67.                 e.printStackTrace();
  68.             }
  69.  
  70.             return null;
  71.         }
  72.  
  73.         @Override
  74.         protected void onPostExecute(Void aVoid) {
  75.             super.onPostExecute(aVoid);
  76.             Toast.makeText(getApplicationContext(),"DownloadCompletato",Toast.LENGTH_LONG).show();}}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement