通常我们安装应用会将应用的地址传给系统安装界面中,然后手动点击安装,要么就是之前我们之前讲的利用静默安装,那么接下来给大家介绍两种通过shell命令来安装apk的方式:
1.安装到系统/data/data下:
public void InstallApk(Context context, final String packagename, final String apkName) {
//首先判断系统中是否已经安装
try {
context.getPackageManager().getPackageInfo(packagename, 0);
Log.d("Installed", "安装了");
} catch (Exception e) {
Log.d("Installed", "未安装");
//apk存放的路径
String apkPath = Environment.getExternalStorageDirectory().getPath() + File.separator + apkName;
File file = new File(apkPath);
if (!file.exists()) {
return;
}
try {
//打开管道,开启可读可写的权限
Runtime.getRuntime().exec("adb shell mount -o remount /system");
//执行强制安装的命令
Process exec = Runtime.getRuntime().exec("adb install -r " + apkPath);
Log.d("Installed", exec.waitFor() + "");
//如果安装成功则删除存放的apk包,0表示成功
if (exec.waitFor() == 0) {
file.delete();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
利用这种方式安装的话,删除应用的方式是:
try {
//打开管道,开启可读可写的权限
Runtime.getRuntime().exec("adb shell mount -o remount /system");
Runtime.getRuntime().exec("adb shell uninstall " + packagename);
} catch (Exception ex) {
ex.printStackTrace();
}
2.安装到/system/app中,作为一个系统级别的应用安装:
public void InstallApk(Context context, final String packagename, final String apkName) {
//首先判断系统中是否已经安装
try {
context.getPackageManager().getPackageInfo(packagename, 0);
Log.d("Installed", "安装了");
} catch (Exception e) {
Log.d("Installed", "未安装");
//apk存放的路径
String apkPath = Environment.getExternalStorageDirectory().getPath() + File.separator + apkName;
File file = new File(apkPath);
if (!file.exists()) {
return;
}
try {
//打开管道,开启可读可写的权限
Runtime.getRuntime().exec("adb shell mount -o remount /system");
//执行拷贝安装的命令
Process exec = Runtime.getRuntime().exec("adb push " + apkPath + " /system/app");
Log.d("Installed", exec.waitFor() + "");
//如果安装成功则删除存放的apk包,0表示成功
if (exec.waitFor() == 0) {
file.delete();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
利用这种方式安装的话,删除应用的方式是:
try {
//打开管道,开启可读可写的权限
Runtime.getRuntime().exec("adb shell mount -o remount /system");
Runtime.getRuntime().exec("adb shell rm -rf /system/app/" + apkName + ".apk");
} catch (Exception ex) {
ex.printStackTrace();
}