您的位置:首页 > 移动开发 > Android开发

android静默安装与静默卸载

2017-10-25 10:18 99 查看

android5.0和6.0静默安装与卸载

android5.0使用shell命令实现静默安装和卸载

废话不多说直接上代码

//静默安装
private void installSpecific(String path) {//apk存储路径
ShellUtils.execCommand("chmod 777 " + path, false);
ShellUtils.CommandResult res = ShellUtils.execCommand("pm install -r " + pkg.getFilePath(), false);//静默安装的shell命令
ShellUtils.CommandResult result = ShellUtils.execCommand("pm uninstall " + "包名", false);//静默卸载的shell命令
if (res.result == 0 && !TextUtils.isEmpty(res.successMsg) && res.successMsg.equals("Success")) {
//安装成功
} else {
//安装失败
}
}


ShellUtils工具类代码

public class ShellUtils {

public static final String COMMAND_SU = "can";
public static final String COMMAND_SH = "sh";
public static final String COMMAND_EXIT = "exit\n";
public static final String COMMAND_LINE_END = "\n";

private ShellUtils() {
throw new AssertionError();
}

/**
* check whether has root permission
*
* @return
*/
public static boolean checkRootPermission() {
return execCommand("echo root", true, false).result == 0;
}

/**
* execute shell command, default return result msg
*
* @param command
*            command
* @param isRoot
*            whether need to run with root
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(String command, boolean isRoot) {
return execCommand(new String[] { command }, isRoot, true);
}

/**
* execute shell commands, default return result msg
*
* @param commands
*            command list
* @param isRoot
*            whether need to run with root
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(List<String> commands, boolean isRoot) {
return execCommand(commands == null ? null : commands.toArray(new String[] {}), isRoot, true);
}

/**
* execute shell commands, default return result msg
*
* @param commands
*            command array
* @param isRoot
*            whether need to run with root
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(String[] commands, boolean isRoot) {
return execCommand(commands, isRoot, true);
}

/**
* execute shell command
*
* @param command
*            command
* @param isRoot
*            whether need to run with root
* @param isNeedResultMsg
*            whether need result msg
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(String command, boolean isRoot, boolean isNeedResultMsg) {
return execCommand(new String[] { command }, isRoot, isNeedResultMsg);
}

/**
* execute shell commands
*
* @param commands
*            command list
* @param isRoot
*            whether need to run with root
* @param isNeedResultMsg
*            whether need result msg
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(List<String> commands, boolean isRoot, boolean isNeedResultMsg) {
return execCommand(commands == null ? null : commands.toArray(new String[] {}), isRoot, isNeedResultMsg);
}

/**
* execute shell commands
*
* @param commands
*            command array
* @param isRoot
*            whether need to run with root
* @param isNeedResultMsg
*            whether need result msg
* @return <ul>
*         <li>if isNeedResultMsg is false, {@link CommandResult#successMsg}
*         is null and {@link CommandResult#errorMsg} is null.</li>
*         <li>if {@link CommandResult#result} is -1, there maybe some
*         excepiton.</li>
*         </ul>
*/
public static CommandResult execCommand(String[] commands, boolean isRoot, boolean isNeedResultMsg) {
int result = -1;
if (commands == null || commands.length == 0) {
return new CommandResult(result, null, null);
}

Process process = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = null;
StringBuilder errorMsg = null;

DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);
os = new DataOutputStream(process.getOutputStream());
for (String command : commands) {
if (command == null) {
continue;
}

// donnot use os.writeBytes(commmand), avoid chinese charset
// error
os.write(command.getBytes());
os.writeBytes(COMMAND_LINE_END);
os.flush();
}
os.writeBytes(COMMAND_EXIT);
os.flush();

result = process.waitFor();
// get command result
if (isNeedResultMsg) {
successMsg = new StringBuilder();
errorMsg = new StringBuilder();
successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String s;
while ((s = successResult.readLine()) != null) {
successMsg.append(s);
}
while ((s = errorResult.readLine()) != null) {
errorMsg.append(s);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
if (successResult != null) {
successResult.close();
}
if (errorResult != null) {
errorResult.close();
}
} catch (IOException e) {
e.printStackTrace();
}

if (process != null) {
process.destroy();
}
}
return new CommandResult(result, successMsg == null ? null : successMsg.toString(), errorMsg == null ? null
: errorMsg.toString());
}

/**
* result of command
* <ul>
* <li>{@link CommandResult#result} means result of command, 0 means normal,
* else means error, same to excute in linux shell</li>
* <li>{@link CommandResult#successMsg} means success message of command
* result</li>
* <li>{@link CommandResult#errorMsg} means error message of command result</li>
* </ul>
*
* @author <a href="http://www.trinea.cn" target="_blank">Trinea</a>
*         2013-5-16
*/
public static class CommandResult {

/** result of command **/
public int result;
/** success message of command result **/
public String successMsg;
/** error message of command result **/
public String errorMsg;

public CommandResult(int result) {
this.result = result;
}

public CommandResult(int result, String successMsg, String errorMsg) {
this.result = result;
this.successMsg = successMsg;
this.errorMsg = errorMsg;
}
}
}


android6.0可以使用shell命令进行静默安装,但是静默卸载得用反射

我们直接把IPackageDeleteObserver和.IPackageInstallObserver类复制出来放到我们的工程中,注意包名要和系统文件包名android.content.pm保持一致,然后使用反射静默卸载,上代码

PackageManager pm = mContext.getPackageManager();
Method[] methods = pm != null ? pm.getClass().getDeclaredMethods() : null;
Method mDel = null;
if (methods != null && methods.length > 0) {
for (Method method : methods) {
if (method.getName().toString().equals("deletePackage")) {
mDel = method;
break;
}
}
}
if (mDel != null) {
mDel.setAccessible(true);
mDel.invoke(pm, pkg.getPackageName(), new IPackageDeleteObserver.Stub() {

@Override
public void packageDeleted(String packageName, int returnCode) throws RemoteException {
Log.i("appstore", "del apk >>" + packageName + ";code >>" + returnCode);
if (!pkg.getPackageName().equals(packageName)) {
return;
}
if (returnCode != 0) {
ToastUtils.show(mContext, pkg.getAppName() + mContext.getString(R.string
.uninstall_success));
} else {
ToastUtils.show(mContext, pkg.getAppName() + mContext.getString(R.string
.install_success));
}
}
}, 0);


添加系统权限

在系统上安装程序是需要注册权限的,在Manifest文件中加入如下语句:

<uses-permission android:name="android.permission.INSTALL_PACKAGES" />


由于然后同样是manifest文件,在节点中加入sharedUserId属性:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.slienceinstall"
android:sharedUserId="android.uid.system"          //声明为系统应用
android:versionCode="1"
android:versionName="1.0" >
... ...
<manifest/>


必须得有系统签名,应用的静默安装和卸载才能成功
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息