标签:MyLog process ---- 静默 TAG line null 安装
最近在做有关静默安装和静默卸载的功能,6.0之前可以使用:
String[] args = null;
if (loc == InsLoc.EXT) {
args = new String[]{ _pm, _install, __r, __s, apkAbsolutePath };
} else if (loc == InsLoc.INT){
args = new String[]{ _pm, _install, __r, __f, apkAbsolutePath };
} else {
args = new String[]{ _pm, _install, __r, apkAbsolutePath };
}
String result = "";
ProcessBuilder processBuilder = new ProcessBuilder(args);
Process process = null;
DataInputStream errIs = null;
DataInputStream inIs = null;
try {
process = processBuilder.start();
MyLog.d(TAG, "----install before waitfor----");
int r = process.waitFor();
MyLog.d(TAG, "----install after waitfor----r:" + r);
errIs = new DataInputStream(process.getErrorStream());
String line = null;
while ((line = errIs.readLine()) != null) {
MyLog.d(TAG, "----err line:[" + line + "]----");
if (line.contains(_Failure)) {
result = line;
}
}
inIs = new DataInputStream(process.getInputStream());
line = null;
while ((line = inIs.readLine()) != null) {
MyLog.d(TAG, "----std line:[" + line + "]----");
if (line.contains(_Success)) {
result = line;
}
}
} catch (IOException e) {
MyLog.w(TAG, "----install ERROR:" + e.getMessage());
} catch (Exception e) {
MyLog.w(TAG, "----install ERROR:" + e.getMessage());
} finally {
try {
if (errIs != null) {
errIs.close();
}
if (inIs != null) {
inIs.close();
}
} catch (IOException e) {
}
if (process != null) {
process.destroy();
}
}
能够正常静默,必须使用root样机。
当放到7.0样机上时,发现静默不了:出现java.lang.SecurityException: Package xxxx does not belong to 10054异常。通过不断的google和度娘,最终使用下面的方法解决:
String result = "";
// ProcessBuilder processBuilder = new ProcessBuilder(args);
Process process = null;
DataInputStream errIs = null;
DataInputStream inIs = null;
try {
process = Runtime.getRuntime().exec("pm install -i "+packageName+" --user 0 "+apkAbsolutePath);
MyLog.d(TAG, "----install before waitfor----");
int r = process.waitFor();
MyLog.d(TAG, "----install after waitfor----r:" + r);
errIs = new DataInputStream(process.getErrorStream());
String line = null;
while ((line = errIs.readLine()) != null) {
MyLog.d(TAG, "----err line:[" + line + "]----");
if (line.contains(_Failure)) {
result = line;
}
}
inIs = new DataInputStream(process.getInputStream());
line = null;
while ((line = inIs.readLine()) != null) {
MyLog.d(TAG, "----std line:[" + line + "]----");
if (line.contains(_Success)) {
result = line;
}
}
} catch (IOException e) {
MyLog.w(TAG, "----install ERROR:" + e.getMessage());
} catch (Exception e) {
MyLog.w(TAG, "----install ERROR:" + e.getMessage());
} finally {
try {
if (errIs != null) {
errIs.close();
}
if (inIs != null) {
inIs.close();
}
} catch (IOException e) {
}
if (process != null) {
process.destroy();
}
}
return result;
值得注意的是,上面的package指的是自身应用的package。当时就因为这个package踩了不少坑。
有关静默卸载:6.0以下使用:
String[] args = null;
if (keepData){
args = new String[]{ _pm, _uninstall, __k, packageName };
}else {
args = new String[]{ _pm, _uninstall, packageName };
}
String result = "";
ProcessBuilder processBuilder = new ProcessBuilder(args);
Process process = null;
InputStream errIs = null;
InputStream inIs = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int read = -1;
process = processBuilder.start();
errIs = process.getErrorStream();
while ((read = errIs.read()) != -1) {
baos.write(read);
}
baos.write('\n');
inIs = process.getInputStream();
while ((read = inIs.read()) != -1) {
baos.write(read);
}
byte[] data = baos.toByteArray();
result = new String(data);
MyLog.i(TAG, "----unInstallPackage exec cmd finish:[" + result.trim() + "]");
} catch (IOException e) {
MyLog.w(TAG, "----unInstallPackage ERR:" + e.getMessage(), e);
} catch (Exception e) {
MyLog.w(TAG, "----unInstallPackage ERR:" + e.getMessage(), e);
} finally {
try {
if (errIs != null) {
errIs.close();
}
if (inIs != null) {
inIs.close();
}
} catch (IOException e) {
MyLog.w(TAG, "----unInstallPackage WARN:" + e.getClass().getName() + "--" + e.getMessage(), e);
}
if (process != null) {
process.destroy();
}
}
return result;
能够正常在root样机下卸载,但是到了6.0以上的时候。又坑爹了。经过研究可以使用反射:
try {
PackageManager pm = context.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, pkgName,null, 0);
}
} catch (Exception e) {
}
基本就是这样了。~~
标签:MyLog,process,----,静默,TAG,line,null,安装 来源: https://blog.51cto.com/u_12682483/2833039
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。