问题描述
当前设备是root,我安排安装说明。安装成功后,广播无法启动APP,但如果手动安装,可以让广播监控安装状态并启动APP。
/**
* 执行具体的静默安装逻辑,需要手机ROOT。
* <p>
* 安装成功返回true,安装失败返回false。
*/
private static boolean install(File file) {
boolean result = false;
DataOutputStream dataOutputStream = null;
BufferedReader errorStream = null;
try {
// 申请su权限
Process process = Runtime.getRuntime().exec("su");
dataOutputStream = new DataOutputStream(process.getoutputStream());
// 执行pm install命令
String command = "pm install -r " + file.getPath() + "\n";
dataOutputStream.write(command.getBytes(Charset.forName("utf-8")));
dataOutputStream.flush();
dataOutputStream.writeBytes("exit\n");
dataOutputStream.flush();
process.waitFor();
errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String msg = "";
String line;
// 读取命令的执行结果
while ((line = errorStream.readLine()) != null) {
msg += line;
}
// Log.d("TAG","install msg is " + msg);
// 如果执行结果中包含Failure字样就认为是安装失败,否则就认为安装成功
if (!msg.contains("Failure")) {
result = true;
}
} catch (Exception ignored) {
ignored.printstacktrace();
} finally {
try {
if (dataOutputStream != null) {
dataOutputStream.close();
}
if (errorStream != null) {
errorStream.close();
}
} catch (IOException ignored) {
}
}
return result;
}
然后我进行了广播监听,安装状态。
<receiver
android:name=".installreceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
public class installreceiver extends broadcastReceiver {
@Override
public void onReceive(Context context,Intent intent) {
if (TextUtils.equals(intent.getAction(),Intent.ACTION_PACKAGE_ADDED)) {
// 应用安装
// 获取应用包名,和要监听的应用包名做对比
String packName = intent.getData() != null ? intent.getData().getSchemeSpecificPart() : "";
ApkUtils.startAPP(context,packName);
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)