国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android實現(xiàn)靜默安裝的兩種方法

2019-12-12 03:39:05
字體:
供稿:網(wǎng)友

前言

一般情況下,Android系統(tǒng)安裝apk會出現(xiàn)一個安裝界面,用戶可以點擊確定或者取消來進(jìn)行apk的安裝。 但在實際的項目需求中,有一種需求,就是希望apk在后臺安裝(不出現(xiàn)安裝界面的提示),這種安裝方式稱為靜默安裝。下面這篇文章就給大家介紹了兩種方法來實現(xiàn),下面來一起看看吧。

1、root權(quán)限靜默安裝實現(xiàn)

實現(xiàn)實際使用的是su pm install -r filePath命令。

核心代碼如下:

protected static void excuteSuCMD() {    Process process = null;    OutputStream out = null;    InputStream in = null;    String currentTempFilePath = "/sdcard/QQ.apk";    try {    // 請求root    process = Runtime.getRuntime().exec("su");    out = process.getOutputStream();    // 調(diào)用安裝    out.write(("pm install -r " + currentTempFilePath + "/n").getBytes());    in = process.getInputStream();    int len = 0;    byte[] bs = new byte[256];    while (-1 != (len = in.read(bs))) {    String state = new String(bs, 0, len);    if (state.equals("Success/n")) {     //安裝成功后的操作      }     }    } catch (IOException e) {     e.printStackTrace();    } catch (Exception e) {     e.printStackTrace();    } finally {     try {      if (out != null) {       out.flush();       out.close();      }      if (in != null) {       in.close();      }     } catch (IOException e) {      e.printStackTrace();     }    }   } 

2、非root權(quán)限提示用戶安裝,代碼如下:

public static void openFile() {   // 核心是下面幾句代碼   if (!isHasfile()) {    downLoadFile(url);   }   Intent intent = new Intent();   intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   intent.setAction(android.content.Intent.ACTION_VIEW);   intent.setDataAndType(     Uri.fromFile(new File("/sdcard/update/updata.apk")),     "application/vnd.android.package-archive");   mContext.startActivity(intent);  } 

總結(jié)

以上就是關(guān)于Android實現(xiàn)靜默安裝的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。謝謝大家對武林網(wǎng)的支持。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 嘉祥县| 都兰县| 安远县| 龙胜| 雷山县| 青阳县| 宽甸| 额济纳旗| 望谟县| 新宾| 麻阳| 普定县| 资兴市| 山东| 汉沽区| 玛纳斯县| 彝良县| 社会| 九寨沟县| 潍坊市| 达拉特旗| 淳化县| 上虞市| 麟游县| 安达市| 昆明市| 柏乡县| 承德县| 镇宁| 朝阳区| 五峰| 什邡市| 宜良县| 桃源县| 鄄城县| 库伦旗| 蓬溪县| 托里县| 宁德市| 昌吉市| 清涧县|