老大要我弄个自动更新,要用到静默安装,网上找到了些大拿的代码,我拿去改吧改吧,先贴出来:
Java代码
- /**
- * 软件静默安装
- * @param apkAbsolutePath apk文件所在路径
- * @return 安装结果:获取到的result值<br>
- *
- * 如果安装成功的话是“
- * pkg: /data/local/tmp/Calculator.apk /nSuccess”,<br>
- * 如果是失败的话,则没有结尾的“Success”。
- */
- public String silentInstall(String apkAbsolutePath) {
- String[] args = { "pm", "install", "-r", apkAbsolutePath };
- 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".getBytes());
- inIs = process.getInputStream();
- while ((read = inIs.read()) != -1) {
- baos.write(read);
- }
- byte[] data = baos.toByteArray();
- result = new String(data);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (errIs != null) {
- errIs.close();
- }
- if (inIs != null) {
- inIs.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- if (process != null) {
- process.destroy();
- }
- }
- return result;
- }
别急,没完,记得加权限呢:
Java代码
- <!-- 以下是静默安装apk所需要到权限 -->
- <uses-permission android:name="android.permission.INSTALL_PACKAGES" />
- <uses-permission android:name="android.permission.DELETE_PACKAGES" />
- <uses-permission android:name="android.permission.CLEAR_APP_CACHE" />
- <uses-permission android:name="android.permission.CLEAR_APP_USER_DATA" />
- <uses-permission android:name="android.permission.READ_PHONE_STATE" />
我粘贴过去,哇靠,好多红茶茶~怎么回事,鼠标移上去一看,
Java代码
- permission is only granted to system apps
奇怪类,我这个就是系统应用阿~~找了好久,听见有个声音说要clean下,于是,我clean了一下,
挖靠!@果然没有红茶茶类~后来不知怎么改了下mainfest,又红茶茶类,我又clean了一下,竟然还有用!所以,以后出错了记得先clean下哦。当然,你要是布局文件有错到话那就没办法了,你需要先让布局文件到错误消失掉才能clean。
时间: 2024-11-10 14:13:30