Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends ActionBarActivity {
- String myHTTPUrl = "http://infodeveloper.altervista.org/mAh/current/mAh.apk";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Button btnTrad, btnDM;
- btnTrad = (Button)findViewById(R.id.btnTrad);
- btnTrad.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- new MyTask().execute();
- }
- });}
- public class MyTask extends AsyncTask<Void, Void, Void>{
- @Override
- protected Void doInBackground(Void... params) {
- try {
- URL myurl = new URL (myHTTPUrl);
- HttpURLConnection connection = (HttpURLConnection)myurl.openConnection();
- connection.setDoOutput(true);
- connection.setRequestMethod("GET");
- connection.connect();
- File rootDirectory = new File (Environment.getExternalStoragePublicDirectory(
- Environment.DIRECTORY_DOWNLOADS), "MieiDownload");
- if (!rootDirectory.exists()) {
- rootDirectory.mkdirs();
- }
- String nameOfFile = URLUtil.guessFileName(myHTTPUrl, null,
- MimeTypeMap.getFileExtensionFromUrl(myHTTPUrl));
- File file = new File (rootDirectory, nameOfFile);
- file.createNewFile();
- InputStream inputStream = connection.getInputStream();
- FileOutputStream output = new FileOutputStream(file);
- byte[] buffer = new byte [1024];
- int byteCount = 0;
- while((byteCount = inputStream.read(buffer))> 0){
- output.write(buffer, 0, byteCount);
- }
- output.close();
- Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
- intent.setData(Uri.fromFile(file));
- getApplicationContext().sendBroadcast(intent);
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- @Override
- protected void onPostExecute(Void aVoid) {
- super.onPostExecute(aVoid);
- Toast.makeText(getApplicationContext(),"DownloadCompletato",Toast.LENGTH_LONG).show();}}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement