Guest User

Untitled

a guest
Oct 1st, 2010
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1.  
  2. private class DownloadTask extends AsyncTask<String, Void, Void> {
  3.         private File PATH;
  4.         private File file;
  5.  
  6.         // make sure you change Main to your activity name.
  7.         private final ProgressDialog dialog = new ProgressDialog(Main.this);
  8.  
  9.         @Override
  10.         protected Void doInBackground(String... params) {
  11.             // TODO Auto-generated method stub
  12.             HttpPost httppost = new HttpPost(updateURL);
  13.             HttpClient httpclient = new DefaultHttpClient();
  14.  
  15.             try {
  16.  
  17.                 // Execute HTTP Post Request
  18.  
  19.                 HttpResponse response = httpclient.execute(httppost);
  20.  
  21.                 StatusLine statusLine = response.getStatusLine();
  22.                 int responseCode = statusLine.getStatusCode();
  23.  
  24.                 InputStream in;
  25.                 HttpEntity entity;
  26.                 if (responseCode == 200) {
  27.                     entity = response.getEntity();
  28.                     in = entity.getContent();
  29.                    
  30.  
  31.                     if (android.os.Environment.getExternalStorageState()
  32.                             .equals(android.os.Environment.MEDIA_MOUNTED))
  33.                         PATH = new File(
  34.                                 android.os.Environment
  35.                                 .getExternalStorageDirectory(),
  36.                         "android/data/com.example.app/cache/");
  37.                     else
  38.                         PATH = getApplicationContext().getCacheDir();
  39.                     if (!PATH.exists())
  40.                         PATH.mkdirs();
  41.                     file = new File(PATH, "MyUpdate.apk");
  42.  
  43.                     OutputStream os = new FileOutputStream(file);
  44.  
  45.                     // this function is from Fedor's Utils class in his lazy list demo, posted at:     
  46.                     // http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview
  47.                     CopyStream(in, os);
  48.                     os.close();
  49.  
  50.                 }
  51.  
  52.             } catch (ClientProtocolException e) {
  53.                 // TODO Auto-generated catch block
  54.             } catch (IOException e) {
  55.                 // TODO Auto-generated catch block
  56.             }
  57.             return null;
  58.         }
  59.  
  60.         @Override
  61.         protected void onPostExecute(final Void unused) {
  62.             if (update) {
  63.                 Intent intent = new Intent();
  64.                 Uri uri = Uri.fromFile(file);
  65.                 intent.setAction(android.content.Intent.ACTION_VIEW);
  66.                 intent.setDataAndType(Uri.parse(uri.toString()),
  67.                 "application/vnd.android.package-archive");
  68.                 startActivity(intent);
  69.                 dialog.dismiss();
  70.             }
  71.  
  72.         }
  73.  
  74.         @Override
  75.         protected void onPreExecute() {
  76.             this.dialog.setMessage("Downloading...");
  77.             this.dialog.show();
  78.  
  79.         }
  80.  
  81.     }
  82.  
  83.  
  84.  
  85.  
  86.     public static void CopyStream(InputStream is, OutputStream os) {
  87.         final int buffer_size = 1024;
  88.         try {
  89.             byte[] bytes = new byte[buffer_size];
  90.             for (;;) {
  91.                 int count = is.read(bytes, 0, buffer_size);
  92.                 if (count == -1)
  93.                     break;
  94.                 os.write(bytes, 0, count);
  95.             }
  96.         } catch (Exception ex) {
  97.         }
  98.     }
Advertisement
Add Comment
Please, Sign In to add comment