Android Program Implementation: Install and uninstall

Source: Internet
Author: User
Installation:

String STR = "/canavacancel.apk"; string filename = environment. getexternalstoragedirectory () + STR;
Intent intent = new intent (intent. action_view );
Intent. 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); environment has some methods for obtaining environment variables package: COM. demo. canavacancel is a package: the complete program path (package name + program name ).

Download APK 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 );
}

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.