静默安装,不稳定,是为何?

中才德创 2016-01-06 02:12:30
核心代码就是下面两行:
机器拥有root权限,升级时,有时正常升上去了,有时黑屏。
String cmd = "pm install -r " + filePath;
Runtime.getRuntime().exec(cmd);

/**
* install slient
* @param filePath
* @return 0 file not exist , 1 success , -1 other exception error
*/
private int installSlient(String filePath) {
try {
if (filePath==null || filePath.length()==0) {
return 0;
}

File file = new File(filePath);
if (!file.exists() || !file.isFile() || file.length()<=0 ) {
return 0;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}

try {
String cmd = "pm install -r " + filePath;
Runtime.getRuntime().exec(cmd);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return -2;
}

return 1;
}

/**
* install slient
* @param filePath
* @return 0 file not exist , 1 success , -1 other exception error
*/
private int installNoSilent(String filePath) {
try {
if (filePath==null || filePath.length()==0) {
return 0;
}

File file = new File(filePath);
if (!file.exists() || !file.isFile() || file.length()<=0 ) {
return 0;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}

try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.parse("file://" + filePath),
"application/vnd.android.package-archive");
this.context.startActivity(intent);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return -2;
}

return 1;
}
...全文
271 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
小马哥_3276 2017-04-06
  • 打赏
  • 举报
回复
pm install命令有时候会报错,可以参考这个博客,可以完美解决这个问题 http://blog.csdn.net/mtx_03/article/details/69388208
中才德创 2016-01-28
  • 打赏
  • 举报
回复
将安装的程序,放在主线程里运行,执行了安装命令后,主线程休眠一段时间。卡死在2~3间,现在设置3秒,未必reboot肯定执行。延时reboot,又无法做到。
/**
	 * install apk
	 */
	public void installApk() {
		//new installApkThread().start();
		if ((downLoadStorageFileDir!=null) && (apkName!=null)) {
			String filePath = downLoadStorageFileDir + File.separatorChar + apkName;
			AvcLog.printW(TAG, "start install apk : " + filePath);
			int iRet = installSlientEx(filePath);
			AvcLog.printW(TAG, "finish install apk, reboot : the result is " + iRet);
			AvcApp.the().getCore().onReboot();
		} else {
			AvcLog.printW(TAG, "installApkThread : path is null");
		}
	}

/**
	 * install slient
	 * @param filePath
	 * @return 0 file not exist , 1 success , -1 other exception error
	 */
	private int installSlientEx(String filePath) {
		try {
			if (filePath==null || filePath.length()==0) {
				return 0;
			}
			
			File file = new File(filePath);
			if (!file.exists() || !file.isFile()  || file.length()<=0 ) {
				return 0;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}
		
		Process proc = null;
		try {
			String cmd = "pm install -r " + filePath;
			Runtime runtime = Runtime.getRuntime();
			AvcLog.printW(TAG, "post cmd : " + filePath);
			proc = runtime.exec(cmd);
			Thread.sleep(3000);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return -2;
		} finally {
			if (proc != null) {
				proc.destroy();
			}
		}

		return 1;
	}

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧