如果是做指定设备开发的,一般设备都有root,这个时候就可以用shell命令安装了,安装代码如下(filePath 为安装包路径):
String[] cmd0 = {"pm install -r " + filePath + "\n"};
ShellUtil.execCommand(cmd0, true);
ShellUtil工具类:
public class ShellUtil {
private final static String COMMAND_SU = "su";
private final static String COMMAND_SH = "sh";
private final static String COMMAND_EXIT = "exit\n";
private final static String COMMAND_LINE_END = "\n";
public static class CommandResult {
public int result = -1;
public String errorMsg;
public String successMsg;
}
public static ShellUtil.CommandResult execCommand(String[] commands, boolean isRoot) {
ShellUtil.CommandResult commandResult = new ShellUtil.CommandResult();
if (commands == null || commands.length == 0) return commandResult;
Process process = null;
DataOutputStream os = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = null;
StringBuilder errorMsg = null;
try {
process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);
os = new DataOutputStream(process.getOutputStream());
for (String command : commands) {
if (command != null) {
os.write(command.getBytes());
os.writeBytes(COMMAND_LINE_END);
os.flush();
}
}
os.writeBytes(COMMAND_EXIT);
os.flush();
commandResult.result = process.waitFor();
//获取错误信息
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);
commandResult.successMsg = successMsg.toString();
commandResult.errorMsg = errorMsg.toString();
// Log.d(TAG, commandResult.result + " | " + commandResult.successMsg
// + " | " + commandResult.errorMsg);
} catch (IOException e) {
String errmsg = e.getMessage();
if (errmsg != null) {
Log.e("util", errmsg);
} else {
e.printStackTrace();
}
} catch (Exception e) {
String errmsg = e.getMessage();
if (errmsg != null) {
Log.e("util", errmsg);
} else {
e.printStackTrace();
}
} finally {
try {
if (os != null) os.close();
if (successResult != null) successResult.close();
if (errorResult != null) errorResult.close();
} catch (IOException e) {
String errmsg = e.getMessage();
if (errmsg != null) {
Log.e("util", errmsg);
} else {
e.printStackTrace();
}
}
if (process != null) process.destroy();
}
return commandResult;
}
}
安装完之后再监听系统的安装广播,自动打开APP:
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction()) || Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())) {
String localPkgName = context.getPackageName();
//取得MyReceiver所在的App的包名
Uri data = intent.getData();
String installedPkgName = data.getSchemeSpecificPart();
if (installedPkgName.equals(localPkgName)) {
Intent bootIntent = context
.getPackageManager()
.getLaunchIntentForPackage(context.getPackageName());
bootIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(bootIntent);
}
}
}
}
在AndroidManifest.xml文件注册静态广播:
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
这样就大功告成啦!
shell功能是很强大的,很多功能都能用shell命令实现,比如修改设备时间,以太网IP,设备重启等