Difference between the two
- Perform normal installation, uninstall, will pop up confirm installation, uninstall the prompt box, and in the File Manager open APK file implementation installation, uninstall the same.
- Perform silent installation, uninstall, normal state, the foreground no response, the app in the background to complete the installation and uninstall. This feature is also commonly referred to as "background installation".
General Installation
Core code:
new Intent(Intent.ACTION_VIEW);intent.setDataAndType( Uri.fromFile(new File(apkPath)), "application/vnd.android.package-archive");context.startActivity(intent);
Normal Uninstall
Core code:
Uri packageURI = Uri.parse("package:"new Intent(Intent.ACTION_DELETE, packageURI);context.startActivity(intent);
In the code above, PackageName is the package name of the target app.
Silent Installation
Core code:
privatestaticfinal"pm install -r ";
String installcmd = Silent_install_cmd + Apkpath;The //PM instruction does not support Chineseintresult =-1;D Ataoutputstream dos =NULL; Process Process =NULL;Try{process = Runtime.getruntime (). EXEC ("Su"); DOS =NewDataOutputStream (Process.getoutputstream ()); Dos.writebytes (Installcmd +"\ n"); Dos.flush (); Dos.writebytes ("exit\n"); Dos.flush (); Process.waitfor (); result = Process.exitvalue ();}Catch(Exception e) {E.printstacktrace ();}finally{Try{if(Dos! =NULL) {dos.close (); }if(Process! =NULL) {Process.destroy (); } }Catch(IOException e) {E.printstacktrace (); }}returnResult
Silent Uninstallation
Core code:
// 如果要保留数据,需要加-k参数,但是卸载会不完全privatestaticfinal"pm uninstall ";
String uninstallcmd = Silent_uninstall_cmd + apppackagename;intresult =-1;D Ataoutputstream dos =NULL; Process Process =NULL;Try{process = Runtime.getruntime (). EXEC ("Su"); DOS =NewDataOutputStream (Process.getoutputstream ()); Dos.writebytes (Uninstallcmd +"\ n"); Dos.flush (); Dos.writebytes ("exit\n"); Dos.flush (); Process.waitfor (); result = Process.exitvalue ();}Catch(Exception e) {E.printstacktrace ();}finally{Try{if(Dos! =NULL) {dos.close (); }if(Process! =NULL) {Process.destroy (); } }Catch(IOException e) {E.printstacktrace (); }}returnResult
In the code above, AppPackageName is the package name of the target app.
More information can be found in this page install
, uninstall
, silentInstall
and the silentUninstall
four methods.
Java code Implementation of the app general security loading and unloading load