Android code to install and uninstall an APK File

Source: Internet
Author: User
AndroidProgramUseCode!!!  Installation:

String STR = "/canavacancel.apk "; String filename = environment. getexternalstoragedirectory () + STR; Intent intent = new intent (intent. action_view );   I Ntent. setdataandtype (URI. fromfile (new file (filename), "application/vnd. Android. Package-Archive "); Startactivity (intent ); Uninstall: Uri packageuri = URI. parse ("package: COM. Demo. canavacancel"); intent uninstallintent = new intent (intent. action_delete, packageuri); startactivity (uninstallintent ); Environmen T has some methods to get environment variables. Package: COM. Demo. canavacancel. This form is package: the complete program path (package name + program name ).

// Download the APK program code
Protected file downloadfile (string httpurl ){
// Todo auto-generated method stub
Final string filename = "updata.apk ";
File tmpfile = new file ("/sdcard/update ");
If (! Tmpfile. exists ()){
Tmpfile. mkdir ();
}
Final file = new file ("/sdcard/update/" + filename );

Try {
URL url = new URL (httpurl );
Try {
Httpurlconnection conn = (httpurlconnection) URL
. Openconnection ();
Inputstream is = conn. getinputstream ();
Fileoutputstream Fos = new fileoutputstream (File );
Byte [] Buf = new byte [1, 256];
Conn. Connect ();
Double COUNT = 0;
If (conn. getresponsecode () & gt; = 400 ){
Toast. maketext (main. This, "connection timeout", Toast. length_short)
. Show ();
} Else {
While (count <= 100 ){
If (is! = NULL ){
Int numread = is. Read (BUF );
If (numread <= 0 ){
Break;
} Else {
FOS. Write (BUF, 0, numread );
}

} Else {
Break;
}

}
}

Conn. disconnect ();
FOS. close ();
is. close ();
}catch (ioexception e) {
// todo auto-generated Catch Block

E. printstacktrace ();
}
} Catch (malformedurlexception e ){
// Todo auto-generated Catch Block

E. printstacktrace ();
}

Return file;
}
// Open the APK program code

Private void openfile (File file ){
// Todo auto-generated method stub
Log. E ("openfile", file. getname ());
Intent intent = new intent ();
Intent. addflags (intent. flag_activity_new_task );
Intent. setaction (Android. content. Intent. action_view );
Intent. setdataandtype (URI. fromfile (file ),
"Application/vnd. Android. Package-Archive ");
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.