Java code Implementation of the app general security loading and unloading load

Source: Internet
Author: User

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

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.