Android實(shí)現(xiàn)靜默安裝的兩種方法
前言
一般情況下,Android系統(tǒng)安裝apk會(huì)出現(xiàn)一個(gè)安裝界面,用戶可以點(diǎn)擊確定或者取消來(lái)進(jìn)行apk的安裝。 但在實(shí)際的項(xiàng)目需求中,有一種需求,就是希望apk在后臺(tái)安裝(不出現(xiàn)安裝界面的提示),這種安裝方式稱為靜默安裝。下面這篇文章就給大家介紹了兩種方法來(lái)實(shí)現(xiàn),下面來(lái)一起看看吧。
1、root權(quán)限靜默安裝實(shí)現(xiàn)
實(shí)現(xiàn)實(shí)際使用的是su pm install -r filePath命令。
核心代碼如下:
protected static void excuteSuCMD() {
Process process = null;
OutputStream out = null;
InputStream in = null;
String currentTempFilePath = "/sdcard/QQ.apk";
try {
// 請(qǐng)求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實(shí)現(xiàn)靜默安裝的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問大家可以留言交流。謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Android布局之GridLayout網(wǎng)格布局
網(wǎng)格布局標(biāo)簽是GridLayout。這個(gè)布局是android4.0新增的布局。這個(gè)布局只有4.0之后的版本才能使用。本文給大家介紹Android布局之GridLayout網(wǎng)格布局相關(guān)知識(shí),感興趣的朋友一起學(xué)習(xí)吧2015-12-12
Android Studio不能獲取遠(yuǎn)程依賴包的完美解決方法
這篇文章主要介紹了Android Studio不能獲取遠(yuǎn)程依賴包的解決方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11
Android實(shí)現(xiàn)微信首頁(yè)左右滑動(dòng)切換效果
這篇文章主要介紹了Android實(shí)現(xiàn)微信首頁(yè)左右滑動(dòng)切換效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
淺談Android應(yīng)用內(nèi)懸浮控件實(shí)踐方案總結(jié)
本篇文章主要介紹了淺談Android應(yīng)用內(nèi)懸浮控件實(shí)踐方案總結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
android canvas drawText()文字居中效果
這篇文章主要為大家詳細(xì)介紹了android canvas drawText()文字居中效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12

