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