Silence as the name implies is silently silent, silent installation and silent uninstall means that in the background silently installed and uninstalled.
One of the most recent apps distributed by the app downloaded the module, after the download is complete, the user can install through this app, in order to improve the user's experience, I added the silent Install and uninstall function, and then joined the use AM command to start an activity.
The Silent Way implementation code for this project is as follows:
First of all to determine whether there is root authority, if there is a quiet way, otherwise use the intention to implement the app installation and uninstall operations.
Package Com.example.test;import Java.io.file;import Java.io.ioexception;import java.io.printwriter;import Android.content.context;import Android.content.intent;import android.net.uri;/** * Description: App install action * @author 吴传龙 * Email:[ email protected] * qq:3026862225 * @version created: March 6, 2015 PM 3:51:14 * @version Last modified: March 6, 2015 3:51:14 Modify a person : Wu Chuanlong */public class Apkcontroller {/** * Description: Installation * Modified by: Wu Chuanlong * Last modified: March 8, 2015 afternoon 9:07:50 */public static Boolean install (S Tring Apkpath,context Context) {//first determine if the phone has root privileges if (Hasrootperssion ()) {//have root privileges, use a silent installation to implement return Clientinstall ( Apkpath);} else{//does not have root privileges and uses intent to install file File = new file (Apkpath); if (!file.exists ()) return false; Intent Intent = new Intent () intent.setaction ("Android.intent.action.VIEW"); Intent.addcategory (" Android.intent.category.DEFAULT "); Intent.setflags (Intent.flag_activity_new_task); Intent.setdataandtype ( Uri.fromfile (file), "application/vnd.android.package-archive"); context.startactivity (intent); return true;}} /** * Description: Uninstall * RepairModified by: Wu Chuanlong * Last modified: March 8, 2015 PM 9:07:50 */public Static Boolean uninstall (String Packagename,context Context) {if (hasrootper Ssion ()) {//have root permission to implement return Clientuninstall (PackageName) with silent uninstallation;} Else{uri Packageuri = Uri.parse ("package:" + packagename); Intent uninstallintent = new Intent (Intent.action_delete, Packageuri); Uninstallintent.setflags (Intent.flag_activity_new_task); context.startactivity (uninstallIntent); return true;}} /** * Determine if the phone has root permissions */private static Boolean hasrootperssion () {printwriter printwriter = null; Process Process = null;try {process = Runtime.getruntime (). EXEC ("su"); PrintWriter = new PrintWriter (Process.getoutputstream ()); Printwriter.flush (); Printwriter.close (); int value = Process.waitfor (); return Returnresult (value);} catch (Exception e) {e.printstacktrace ();} Finally{if (process!=null) {Process.destroy ();}} return false;} /** * Silent Installation */private static Boolean Clientinstall (String apkpath) {printwriter printwriter = null; Process Process = null;try {process = Runtime.getruntime (). EXEC ("su"); PrintWriter = new PrintWriter (Process.getoutputstream ()); Printwriter.println ("chmod 777" +apkpath); PRINTWRITER.PRINTLN ("Export ld_library_path=/vendor/lib:/system/lib"); Printwriter.println ("PM Install-r" +apkpath);//printwriter.println ("Exit"); Printwriter.flush (); Printwriter.close (); int value = Process.waitfor (); return Returnresult (value);} catch (Exception e) {e.printstacktrace ();} Finally{if (process!=null) {Process.destroy ();}} return false;} /** * Silent Unload */private static Boolean Clientuninstall (String packagename) {printwriter printwriter = null; Process Process = null;try {process = Runtime.getruntime (). EXEC ("su"); PrintWriter = new PrintWriter (Process.getoutputstream ()); Printwriter.println ("Ld_library_path=/vendor/lib:/system/lib"); Printwriter.println ("PM Uninstall" +packagename); Printwriter.flush (); Printwriter.close (); int value = Process.waitfor (); return Returnresult (value); } catch (Exception e) {e.printstacktrace ();} Finally{if (process!=null) {Process.destroy ();}} Return false;} /** * Launch app * com.exmaple.client/. Mainactivity * com.exmaple.client/com.exmaple.client.mainactivity */public static Boolean startApp (String packagename , String Activityname) {Boolean issuccess = false; String cmd = "am start-n" + PackageName + "/" + Activityname + "\ n"; Process Process = null;try {process = Runtime.getruntime (). exec (CMD); int value = Process.waitfor (); return Returnresult (value);} catch (Exception e) {e.printstacktrace ();} finally{if (Process!=null) {Process.destroy ();}} return issuccess;} private static Boolean returnresult (int value) {//Represents success if (value = = 0) {return true;} else if (value = = 1) {//Failed return false;} else {//unknown case return false;} }}
Package Com.example.test;import Java.io.file;import Android.support.v4.app.fragment;import android.app.Activity; Import Android.os.bundle;import android.os.environment;import Android.view.layoutinflater;import Android.view.Menu ; Import Android.view.menuitem;import android.view.view;import android.view.viewgroup;import android.widget.Toast; Import android.os.build;/** * Description: Mainactivity * @author Wu Chuanlong * email:[email protected] * qq:3026862225 * @vers Ion creation Time: March 9, 2015 8:19:19 * @version last modified: March 9, 2015 8:19:19 modified by: Wu Chuanlong */public class Mainactivity extends Activit y {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main);} /** * Description: Installation * @param * Modified by: Wu Chuanlong * Last modified: March 9, 2015 morning 8:19:30 */public void Click1 (view view) {new Thread () {public void Ru N () {String path = Environment.getexternalstoragedirectory (). GetAbsolutePath () + "/jnitest.apk"; if ( Apkcontroller.install (Path, Getapplicationcontext ())) {ToAST ("Installation Success");} Else{toast ("Installation Failed");}};}. Start ();} /** * Description: Uninstall * @param * Modified by: Wu Chuanlong * Last modified: March 9, 2015 morning 8:19:30 */public void Click2 (view view) {new Thread () {public void Ru N () {if (Apkcontroller.uninstall ("Com.example.jnitest", Getapplicationcontext ())) {Toast ("unload succeeded");} Else{toast ("Unload Failed");}};}. Start ();} /** * Description: Start * @param * Modified by: Wu Chuanlong * Last modified: March 9, 2015 morning 8:19:30 */public void Click3 (view view) {if (Apkcontroller.startapp ( "Com.example.jnitest", "com.example.jnitest.MainActivity") {Toast ("Activation succeeded");}} public void Toast (final String text) {Runonuithread (new Runnable () {@Overridepublic void run () {Toast.maketext ( Getapplicationcontext (), text, Toast.length_short). Show ();;}});}}
In other ways to achieve the silent way, can be disguised as a system application, which will give the app to the application of the signature, but these signatures in Xiaomi and other mobile phones are useless, so here do not introduce. It is also possible to put the application in the System/app directory.
Android silent installation and Silent uninstallation