Download APK and install code

Source: Internet
Author: User

[Java]
/**
* Download an object
* @ Param url
*/
Private void downFile (final String url ){
MProgressDialog = new ProgressDialog (activity );
MProgressDialog. setProgressStyle (ProgressDialog. STYLE_SPINNER );
MProgressDialog. setMessage ("Please wait ...");
MProgressDialog. show ();
 
New Thread (){
@ Override
Public void run (){
DefaultHttpClient client = new DefaultHttpClient ();
HttpGet = new HttpGet (url );
HttpResponse response;
Try {
Response = client.exe cute (get );
HttpEntity entity = response. getEntity ();
Long length = entity. getContentLength ();
InputStream is = entity. getContent ();
FileOutputStream fileOutputStream = null;
If (is! = Null ){
File file = new File (PATH + software + ". apk ");
FileOutputStream = new FileOutputStream (file );
 
Byte [] buf = new byte [1, 1024];
Int ch =-1;
Int count = 0;
While (ch = is. read (buf ))! =-1 ){
// Baos. write (buf, 0, ch );
FileOutputStream. write (buf, 0, ch );
Count + = ch;
If (length> 0 ){
}
 
}
 
}
FileOutputStream. flush ();
If (fileOutputStream! = Null ){
FileOutputStream. close ();
}
Update ();
} Catch (ClientProtocolException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
 
}. Start ();
 
}
 
Private void update (){
// Log. v ("update", "update ");
Intent intent = new Intent (Intent. ACTION_VIEW );
Intent. setDataAndType (
Uri. fromFile (new File ("file: //" + PATH + software + ". apk ")),
"Application/vnd. android. package-archive ");
 
Activity. startActivity (intent );
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.