Android implementation of silent installation and uninstall method _android

Source: Internet
Author: User
Tags stub

An example of this article describes the way Android implements silent installation and uninstall. Share to everyone for your reference. Specifically as follows:

Method 1: "Using the Call interface method, because the security loading and unloading of the application part of the API is hidden, so must download the Android system source code, under the source code development and compile after the use of the MM command compiled to generate APK files"

Import Java.io.File;
Import android.app.Activity;
Import Android.os.Bundle;
Import android.content.Intent;
Import Android.content.pm.PackageInfo;
Import Android.content.pm.PackageManager;
Import android.content.pm.PackageManager.NameNotFoundException;
Import Android.content.pm.IPackageInstallObserver;
Import Android.content.pm.IPackageDeleteObserver;
Import android.content.res.Resources;
Import android.graphics.drawable.Drawable;
Import Android.net.Uri;
Import android.os.Environment;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.util.Log;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.ProgressBar;
Import Android.widget.TextView;
Import Android.widget.Toast; The public class Installactivity extends activity {/** called the ' when ' is the ' the ' activity ' is the ' the '. * Private String Created
 E = "qqhd-mini.apk";
 Private Button install = null;
 Private Button uninstall = null; Private final int Install_cOmplete = 1;
 Private final int uninstall_complete = 2;
 Final static int succeeded = 1;
 Final static int FAILED = 0;
  public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.install_uninstall);
 Install = (Button) Findviewbyid (R.id.install);
 Uninstall = (Button) Findviewbyid (R.id.uninstall);
  Uninstall.setonclicklistener (Listener1);
Install.setonclicklistener (LISTENER2); Private Onclicklistener listener2=new Onclicklistener () {public void OnClick (View v) {String fileName = Environme 
   Nt.getexternalstoragedirectory () + "/" + apk_name;
   Uri uri = uri.fromfile (new File (FileName));
   int installflags = 0;
   Packagemanager pm = Getpackagemanager ();
    try {packageinfo pi = pm.getpackageinfo ("Com.tencent.minihd.qq", packagemanager.get_uninstalled_packages);
    if (pi!= null) {installflags |= packagemanager.install_replace_existing; } catch (Namenotfoundexception e) {} packageinstallobseRVer Observer = new Packageinstallobserver ();
  Pm.installpackage (URI, Observer, Installflags, "com.tencent.minihd.qq");
 }
 }; Class Packageinstallobserver extends Ipackageinstallobserver.stub {public void packageinstalled (String packagename,
   T returncode) {Message msg = Mhandler.obtainmessage (install_complete);
   MSG.ARG1 = ReturnCode;
  Mhandler.sendmessage (msg);
 }
 }; Private Handler Mhandler = new Handler () {public void Handlemessage (msg) {switch (msg.what) {Case Inst
     All_complete:int Centertextlabel;
     Drawable centertextdrawable = null; if (Msg.arg1 = = succeeded) {Toast.maketext (Getapplicationcontext (), "Install apk succeeded", Toast.length_short). Sho
     W ();
 } else {} if (centertextdrawable!= null) {} break;
     Case Uninstall_complete:int CenterTextLabel2;
     Drawable centerTextDrawable2 = null; if (Msg.arg1 = = succeeded) {Toast.maketext (Getapplicationcontext (), "Uninstall apk succeeded", Toast.
     Length_short). Show (); 
    } else {} if (CenterTextDrawable2!= null) {} break;
   Default:break;
 }
  }
 }; Private Onclicklistener listener1=new Onclicklistener () {public void OnClick (View v) {Packagemanager pm = getpackage
   Manager ();
   Packagedeleteobserver observer = new Packagedeleteobserver ();
  Pm.deletepackage ("COM.TENCENT.MINIHD.QQ", observer, 0);
 }
 }; Private class Packagedeleteobserver extends Ipackagedeleteobserver.stub {public void packagedeleted (String packagename
   , int returncode) {Message msg = Mhandler.obtainmessage (uninstall_complete);
   MSG.ARG1 = ReturnCode;
  Mhandler.sendmessage (msg);

 } 
 }
}

Install, uninstall related permissions:

<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission Android:name= "Android.permission.INSTALL_PACKAGES"/>
<uses-permission android:name= " Android.permission.DELETE_PACKAGES "/>
<uses-permission android:name=" android.permission.CLEAR_APP_ CACHE "/>
<uses-permission android:name=" Android.permission.READ_PHONE_STATE "/>
< Uses-permission android:name= "Android.permission.CLEAR_APP_USER_DATA"/>

Method 2: "You need to obtain root permissions or have the same signature as ROM, so that the APK is running as system

private void Test () {
 execcommand ("system/bin/pm install-r" + "sdcard/xxx.apk");
 XXX.APK placed in the built-in storage root directory (MNT/SDCARD/XXX.APK)
 //ExecCommand ("system/bin/pm Uninstall" + package_name); 
 Package_name is xxx.apk package name
 } public
 boolean execcommand (String cmd) {
 process process = null;
 try {
 process = Runtime.getruntime (). exec (cmd);
 Process.waitfor ();
 } catch (Exception e) {return
 false;
 } finally {
 try {
 Process.destroy ();
 } catch (Exception e) {
 }
 }
 return true;
}

Other ways: Some third-party jar packs, and so on.

I hope this article will help you with your Android program.

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.