調(diào)用系統(tǒng)安裝程序
這種方式最為簡單尝盼,只需要調(diào)起系統(tǒng)界面即可植旧∮睾铮看代碼
/**
* 調(diào)用系統(tǒng)安裝界面
*
* @param context
* @param apkFile
*/
public static void startInstallActivity(Context context, File apkFile) {
if (apkFile == null || !apkFile.exists()) return;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.parse("file://" + apkFile.toString()), "application/vnd.android.package-archive");
context.startActivity(intent);
}
調(diào)用系統(tǒng)pm命令
這種方式是在通過終端執(zhí)行pm命令來實現(xiàn)
pm install -r [filepath]
其中-r
代表覆蓋安裝慕淡。
這種方式可以實現(xiàn)靜默安裝,不需要調(diào)起系統(tǒng)界面沸毁。但是有一個前提峰髓,就是需要root權(quán)限,沒有去root權(quán)限息尺,pm命令不能執(zhí)行携兵。獲取root權(quán)限不是本文范疇,請自行百度或者google搂誉。
滿足前提的情況下徐紧,我們可能用到以下方法。
- 判斷設(shè)備是否有root權(quán)限,通過執(zhí)行su命令是否正確來檢查設(shè)備是否被root
/**
* 檢查設(shè)備是否被root
*
* @return
*/
public static boolean isRoot() {
try {
Process process = Runtime.getRuntime().exec("su");
process.getOutputStream().write("exit\n".getBytes());
process.getOutputStream().flush();
int i = process.waitFor();
if (0 == i) {
process = Runtime.getRuntime().exec("su");
return true;
}
} catch (Exception e) {
return false;
}
return false;
}
- 執(zhí)行終端命令
/**
* 執(zhí)行終端命令
*
* @param cmd
* @return
* @throws IOException
* @throws InterruptedException
*/
public static int execRootCmdSilent(String cmd) throws IOException, InterruptedException {
int result = -1;
DataOutputStream dos = null;
try {
Process p = Runtime.getRuntime().exec("su");
dos = new DataOutputStream(p.getOutputStream());
Log.i(TAG, cmd);
dos.writeBytes(cmd + "\n");
dos.flush();
dos.writeBytes("exit\n");
dos.flush();
p.waitFor();
result = p.exitValue();
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
- 安裝apk,在終端執(zhí)行
pm install -r filePath
public static void installApk(String filePath) throws IOException, InterruptedException {
if (TextUtils.isEmpty(filePath) || !new File(filePath).exists()) return;
execRootCmdSilent("pm install -r " + filePath);
}
反射PackageManager的installPackage方法
通過查看查看系統(tǒng)安裝程序的源碼并级,可以看到系統(tǒng)安裝程序?qū)嶋H上是調(diào)用PackageManager的installPackage方法來實現(xiàn)安裝的拂檩。
/**
* @deprecated replaced by {@link PackageInstaller}
* @hide
*/
@Deprecated
public abstract void installPackage(
Uri packageURI, //APK地址
IPackageInstallObserver observer, //安裝回調(diào)
@InstallFlags int flags,
String installerPackageName); //被安裝APK的包名
installPackage方法雖然是public修飾的,但是同時被@hide了嘲碧,所以應(yīng)用程序無法直接調(diào)用稻励,這就需要用到反射機(jī)制了。
調(diào)用這個方法需要系統(tǒng)權(quán)限呀潭,所以需要用系統(tǒng)簽名對apk打包钉迷,具體操作可以參考這篇文章
-
IPackageInstallObserver是一個AIDL接口,所以我們需要用到IPackageInstallObserver.aidl 這個文件钠署。你可以
直接從系統(tǒng)源碼拷貝,該文件位于
/frameworks/base/core/java/android/content/pm
或者拷貝我的荒椭,在你的項目下創(chuàng)建
app/src/main/aidl/android/content/pm/IPackageInstallObserver.aidl
文件谐鼎,然后將下面的代碼復(fù)制進(jìn)去
package android.content.pm;
/**
* API for installation callbacks from the Package Manager.
* @hide
*/
oneway interface IPackageInstallObserver {
void packageInstalled(in String packageName, int returnCode);
}
當(dāng)上述都做好了之后。
- 調(diào)用安裝的方法
//installPackage方法名
private static final String INSTALL_METHOD = "installPackage";
//InstallFlags
public static final int INSTALL_FORWARD_LOCK = 0x00000001;
public static final int INSTALL_REPLACE_EXISTING = 0x00000002;
public static final int INSTALL_ALLOW_TEST = 0x0000000
public static final int INSTALL_EXTERNAL = 0x00000008;
public static final int INSTALL_INTERNAL = 0x00000010;
public static final int INSTALL_FROM_ADB = 0x00000020;
public static final int INSTALL_ALL_USERS = 0x00000040;
public static final int INSTALL_ALLOW_DOWNGRADE = 0x00000080;
public static final int INSTALL_GRANT_RUNTIME_PERMISSIONS = 0x00000100;
public static final int INSTALL_FORCE_VOLUME_UUID = 0x00000200;
public static final int INSTALL_FORCE_PERMISSION_PROMPT = 0x00000400;
public static final int INSTALL_EPHEMERAL = 0x00000800;
public static final int INSTALL_DONT_KILL_APP = 0x00001000;
public static final int INSTALL_FORCE_SDK = 0x00002000;
public static final int DONT_KILL_APP = 0x00000001;
public static void installPackage(Context context, File file, IPackageInstallObserver observer)
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
PackageManager packageManager = context.getPackageManager();
Method method = PackageManager.class.getDeclaredMethod(INSTALL_METHOD, Uri.class,
IPackageInstallObserver.class, int.class, String.class);
method.invoke(packageManager, Uri.fromFile(file), observer, INSTALL_REPLACE_EXISTING | INSTALL_DONT_KILL_APP, GetAppInfo.getAPKPackageName(context, file.getAbsolutePath()));
}
- 監(jiān)聽安裝回調(diào)
//安裝回調(diào)
private static class PackageInstallObserver extends IPackageInstallObserver.Stub {
@Override
public void packageInstalled(String packageName, int returnCode) throws RemoteException {
if(returnCode == INSTALL_SUCCEEDED){
//安裝成功
}
}
}
//returnCode的取值
public static final int INSTALL_SUCCEEDED = 1;
public static final int INSTALL_FAILED_ALREADY_EXISTS = -1;
public static final int INSTALL_FAILED_INVALID_APK = -2;
public static final int INSTALL_FAILED_INVALID_URI = -3;
public static final int INSTALL_FAILED_INSUFFICIENT_STORAGE = -4;
public static final int INSTALL_FAILED_DUPLICATE_PACKAGE = -5;
public static final int INSTALL_FAILED_NO_SHARED_USER = -6;
public static final int INSTALL_FAILED_UPDATE_INCOMPATIBLE = -7;
public static final int INSTALL_FAILED_SHARED_USER_INCOMPATIBLE = -8;
public static final int INSTALL_FAILED_MISSING_SHARED_LIBRARY = -9;
public static final int INSTALL_FAILED_REPLACE_COULDNT_DELETE = -10;
public static final int INSTALL_FAILED_DEXOPT = -11;
public static final int INSTALL_FAILED_OLDER_SDK = -12;
public static final int INSTALL_FAILED_CONFLICTING_PROVIDER = -13;
public static final int INSTALL_FAILED_NEWER_SDK = -14;
public static final int INSTALL_FAILED_TEST_ONLY = -15;
public static final int INSTALL_FAILED_CPU_ABI_INCOMPATIBLE = -16;
public static final int INSTALL_FAILED_MISSING_FEATURE = -17;
public static final int INSTALL_FAILED_CONTAINER_ERROR = -18;
public static final int INSTALL_FAILED_INVALID_INSTALL_LOCATION = -19;
public static final int INSTALL_FAILED_MEDIA_UNAVAILABLE = -20;
public static final int INSTALL_FAILED_VERIFICATION_TIMEOUT = -21;
public static final int INSTALL_FAILED_VERIFICATION_FAILURE = -22;
public static final int INSTALL_FAILED_PACKAGE_CHANGED = -23;
public static final int INSTALL_FAILED_UID_CHANGED = -24;
public static final int INSTALL_FAILED_VERSION_DOWNGRADE = -25;
public static final int INSTALL_FAILED_PERMISSION_MODEL_DOWNGRADE = -26;
public static final int INSTALL_PARSE_FAILED_NOT_APK = -100;
public static final int INSTALL_PARSE_FAILED_BAD_MANIFEST = -101;
public static final int INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION = -102;
public static final int INSTALL_PARSE_FAILED_NO_CERTIFICATES = -103;
public static final int INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES = -104;
public static final int INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING = -105;
public static final int INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME = -106;
public static final int INSTALL_PARSE_FAILED_BAD_SHARED_USER_ID = -107;
public static final int INSTALL_PARSE_FAILED_MANIFEST_MALFORMED = -108;
public static final int INSTALL_PARSE_FAILED_MANIFEST_EMPTY = -109;
public static final int INSTALL_FAILED_INTERNAL_ERROR = -110;
public static final int INSTALL_FAILED_USER_RESTRICTED = -111;
public static final int INSTALL_FAILED_DUPLICATE_PERMISSION = -112;
public static final int INSTALL_FAILED_NO_MATCHING_ABIS = -113;
public static final int NO_NATIVE_LIBRARIES = -114;
public static final int INSTALL_FAILED_ABORTED = -115;