android 实现静默安装、卸载
方法1:【使用调用接口方法,由于安装卸载应用程序的部分API是隐藏的,所以必须下载Android系统源码,在源码下开发并编译之后使用MM命令编译生成APK文件】
1 import java.io.File; 2 import app.Activity; 3 import android.os.Bundle; 4 import android.content.Intent; 5 import android.content.pm.PackageInfo; 6 import android.content.pm.PackageManager; 7 import android.content.pm.PackageManager.NameNotFoundException; 8 import android.content.pm.IPackageInstallObserver; 9 import android.content.pm.IPackageDeleteObserver; 10 import android.content.res.Resources; 11 import android.graphics.drawable.Drawable; 12 import android.net.Uri; 13 import android.os.Environment; 14 import android.os.Handler; 15 import android.os.Message; 16 import android.util.Log; 17 import android.view.View; 18 import android.view.View.OnClickListener; 19 import android.widget.Button; 20 import android.widget.ProgressBar; 21 import android.widget.TextView; 22 import android.widget.Toast; 23 24 public class InstallActivity extends Activity { 25 /** Called when the activity is first created. */ 26 private String APK_NAME = "QQHD-mini.apk"; 27 private Button install = null; 28 private Button uninstall = null; 29 private final int INSTALL_COMPLETE = 1; 30 private final int UNINSTALL_COMPLETE = 2; 31 final static int SUCCEEDED = 1; 32 final static int FAILED = 0; 33 public void onCreate(Bundle savedInstanceState) { 34 super.onCreate(savedInstanceState); 35 setContentView(R.layout.install_uninstall); 36 install = (Button) findViewById(R.id.install); 37 uninstall = (Button) findViewById(R.id.uninstall); 38 uninstall.setOnClickListener(listener1); 39 install.setOnClickListener(listener2); 40 } 41 private OnClickListener listener2=new OnClickListener(){ 42 public void onClick(View v) { 43 String fileName = Environment.getExternalStorageDirectory() + "/" + APK_NAME; 44 Uri uri = Uri.fromFile(new File(fileName)); 45 46 int installFlags = 0; 47 PackageManager pm = getPackageManager(); 48 try { 49 PackageInfo pi = pm.getPackageInfo("com.tencent.minihd.qq", 50 PackageManager.GET_UNINSTALLED_PACKAGES); 51 if(pi != null) { 52 installFlags |= PackageManager.INSTALL_REPLACE_EXISTING; 53 } 54 } catch (NameNotFoundException e) {} 55 56 PackageInstallObserver observer = new PackageInstallObserver(); 57 pm.installPackage(uri, observer, installFlags, "com.tencent.minihd.qq"); 58 } 59 }; 60 61 class PackageInstallObserver extends IPackageInstallObserver.Stub { 62 public void packageInstalled(String packageName, int returnCode) { 63 Message msg = mHandler.obtainMessage(INSTALL_COMPLETE); 64 msg.arg1 = returnCode; 65 mHandler.sendMessage(msg); 66 } 67 }; 68 69 private Handler mHandler = new Handler() { 70 public void handleMessage(Message msg) { 71 switch (msg.what) { 72 case INSTALL_COMPLETE: 73 int centerTextLabel; 74 Drawable centerTextDrawable = null; 75 if(msg.arg1 == SUCCEEDED) { 76 Toast.makeText(getApplicationContext(), "install apk SUCCEEDED", Toast.LENGTH_SHORT).show(); 77 } else {} 78 if (centerTextDrawable != null) {} 79 80 break; 81 case UNINSTALL_COMPLETE: 82 int centerTextLabel2; 83 Drawable centerTextDrawable2 = null; 84 if(msg.arg1 == SUCCEEDED) { 85 Toast.makeText(getApplicationContext(), "uninstall apk SUCCEEDED", Toast.LENGTH_SHORT).show(); 86 } else {} 87 if (centerTextDrawable2 != null) {} 88 89 break; 90 default: 91 break; 92 } 93 } 94 }; 95 96 private OnClickListener listener1=new OnClickListener(){ 97 public void onClick(View v) { 98 PackageManager pm = getPackageManager(); 99 PackageDeleteObserver observer = new PackageDeleteObserver(); 100 pm.deletePackage("com.tencent.minihd.qq", observer, 0); 101 } 102 }; 103 104 private class PackageDeleteObserver extends IPackageDeleteObserver.Stub { 105 public void packageDeleted(String packageName, int returnCode) { 106 Message msg = mHandler.obtainMessage(UNINSTALL_COMPLETE); 107 msg.arg1 = returnCode; 108 mHandler.sendMessage(msg); 109 } 110 } 111 112 }
安装、卸载相关权限<uses-permission android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android.permission.INSTALL_PACKAGES" />
<uses-permission android.permission.DELETE_PACKAGES" />
<uses-permission android.permission.CLEAR_APP_CACHE" />
<uses-permission android.permission.READ_PHONE_STATE"/>
<uses-permission android.permission.CLEAR_APP_USER_DATA" />
方法2:【需要获取root权限或者和rom拥有一样的签名,即让该apk以System的身份在系统中运行】
1 private void test() { 2 execCommand("system/bin/pm install -r " + "sdcard/xxx.apk"); // xxx.apk放置在内置储存的根目录下(mnt/sdcard/xxx.apk) 3 4 // execCommand("system/bin/pm uninstall " + PACKAGE_NAME); //PACKAGE_NAME为xxx.apk包名 5 } 6 7 public boolean execCommand(String cmd) { 8 Process process = null; 9 try { 10 process = Runtime.getRuntime().exec(cmd); 11 process.waitFor(); 12 } catch (Exception e) { 13 return false; 14 } finally { 15 try { 16 process.destroy(); 17 } catch (Exception e) { 18 } 19 } 20 return true; 21 }
其他方法:一些第三方jar包等方式