Android Silent Installation and uninstallation

Source: Internet
Author: User

Android Silent Installation and uninstallation

Silent means silent and Silent Installation and uninstallation, which means the installation and uninstallation in the background.

The download module of the app in a recent app distribution project. After the download is completed, you can install the app to improve the user experience, I added the Silent Installation and uninstallation functions, and then added the am command to start an Activity.

The silent implementation code in this project is as follows:

First, determine whether the root permission is available. If there is a silent mode, otherwise, use the intent to install and uninstall the app.


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 installation operation * @ author Wu chuanlong * Email: andywuchuanlong@sina.cn * QQ: 3026862225 * @ version Creation Time: March 6, 2015 3:51:14 * @ version last modification time: march 6, 2015 3:51:14 modifier: Wu chuanlong */public class ApkController {/*** Description: Installation * modifier: Wu chuanlong * Last modification time: 9:07:50, January 1, March 8, 2015 */public static boolean install (String apkPath, Context context) {// determine whether the mobile phone has the root permission if (hasRootPerssion ()) {// has root permission. Use Silent Installation to implement return clientInstall (apkPath);} else {// has no root permission. Use the 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 * modifier: Wu chuanlong * last modification time: 9:07:50, January 1, March 8, 2015 */public static Boolean uninstall (String packageName, Context context) {if (hasRootPerssion () {// you have the root permission. Use the silent uninstall function to implement return clientUninstall (packageName );} 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 ;}/ *** determines whether the mobile phone has the root permission */private s Tatic boolean hasRootPerssion () {PrintWriter = null; Process process = null; try {process = runtime.getruntime(cmd.exe c ("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 = null; Process process = null; try {process = runtime.getruntime(cmd.exe c ("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 uninstall */private static boolean clientUninstall (String packageName) {PrintWriter = null; Process process = null; try {process = runtime.getruntime(cmd.exe c ("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;}/*** start 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(cmd.exe c (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) {// if (value = 0) {return true ;} else if (value = 1) {// failed return false;} else {// unknown 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: andywuchuanlong@sina.cn * QQ: 3026862225 * @ version Creation Time: March 9, 2015 8:19:19 * @ version last modification time: march 9, 2015 8:19:19 modifier: Wu chuanlong */public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);}/*** Description: Installation * @ param * modifier: Wu chuanlong * last modification time: March 9, 2015 8:19:30 */public void click1 (View view) {new Thread () {public void run () {String path = Environment. getExternalStorageDirectory (). getAbsolutePath () + "/jniTest.apk"; if (ApkController. install (path, getApplicationContext () {toast ("Security successful");} else {toast ("Security failed ");}};}. start ();}/*** Description: uninstall * @ param * modifier: Wu chuanlong * last modification time: 8:19:30, January 1, March 9, 2015 */public void click2 (View view) {new Thread () {public void run () {if (ApkController. uninstall ("com. example. jnitest ", getApplicationContext () {toast (" ");} else {toast (" ");}};}. start ();}/*** Description: start * @ param * modifier: Wu chuanlong * last modification time: March 9, 2015 8:19:30 */public void click3 (View view) {if (ApkController. startApp ("com. example. jnitest "," com. example. jnitest. mainActivity ") {toast (" successful callback ") ;}} public void toast (final String text) {runOnUiThread (new Runnable () {@ Overridepublic void run () {Toast. makeText (getApplicationContext (), text, Toast. LENGTH_SHORT ). show ();;}});}}

To implement silent mode in other ways, you can pretend to be a system application. This requires the app to be signed by the system application. However, these signatures are useless On Xiaomi and other mobile phones, so we will not introduce it here. You can also put the application in the system/app directory.




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.