Method 1: "Use the Invoke interface method, because the security loading application part of the API is hidden, so you must download the Android system source code, under the source code developed and compiled using the MM command compiled to build the APK file"
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;
public class Installactivity extends Activity {
/** called when the activity is first created. */
Private String Apk_name = "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 = environment.getexternalstoragedirectory () + "/" + apk_name;
Uri uri = uri.fromfile (new File);
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, int returncode) {
Message msg = Mhandler.obtainmessage (install_complete);
MSG.ARG1 = ReturnCode;
Mhandler.sendmessage (msg);
}
};
Private Handler Mhandler = new Handler () {
public void Handlemessage (Message msg) {
Switch (msg.what) {
Case Install_complete:
int Centertextlabel;
Drawable centertextdrawable = null;
if (Msg.arg1 = = succeeded) {
Toast.maketext (Getapplicationcontext (), "Install apk succeeded", Toast.length_short). Show ();
} 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 = Getpackagemanager ();
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: "Need to get root privileges or the same signature as ROM, that is, let the APK run as System"
private void Test () {
ExecCommand ("system/bin/pm install-r" + "sdcard/xxx.apk"); Xxx.apk placed in the root directory of the built-in storage (MNT/SDCARD/XXX.APK)
ExecCommand ("system/bin/pm Uninstall" + package_name); Package_name for 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 packages, etc.
Android for silent installation, uninstall