Android實現(xiàn)靜默安裝的兩種方法
前言
一般情況下,Android系統(tǒng)安裝apk會出現(xià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í)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。謝謝大家對腳本之家的支持。
相關(guān)文章
Android布局之GridLayout網(wǎng)格布局
網(wǎng)格布局標簽是GridLayout。這個布局是android4.0新增的布局。這個布局只有4.0之后的版本才能使用。本文給大家介紹Android布局之GridLayout網(wǎng)格布局相關(guān)知識,感興趣的朋友一起學(xué)習(xí)吧2015-12-12Android Studio不能獲取遠程依賴包的完美解決方法
這篇文章主要介紹了Android Studio不能獲取遠程依賴包的解決方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-11-11淺談Android應(yīng)用內(nèi)懸浮控件實踐方案總結(jié)
本篇文章主要介紹了淺談Android應(yīng)用內(nèi)懸浮控件實踐方案總結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11android canvas drawText()文字居中效果
這篇文章主要為大家詳細介紹了android canvas drawText()文字居中效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12