Android實(shí)現(xiàn)靜默安裝的兩種方法
前言
一般情況下,Android系統(tǒng)安裝apk會(huì)出現(xiàn)一個(gè)安裝界面,用戶(hù)可以點(diǎn)擊確定或者取消來(lái)進(jìn)行apk的安裝。 但在實(shí)際的項(xiàng)目需求中,有一種需求,就是希望apk在后臺(tái)安裝(不出現(xiàn)安裝界面的提示),這種安裝方式稱(chēng)為靜默安裝。下面這篇文章就給大家介紹了兩種方法來(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)限提示用戶(hù)安裝,代碼如下:
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)一定的幫助,如果有疑問(wèn)大家可以留言交流。謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Android 實(shí)現(xiàn)定時(shí)任務(wù)的過(guò)程詳解
這篇文章主要介紹了Android 定時(shí)任務(wù)過(guò)程詳解的相關(guān)資料,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Android自定義View實(shí)現(xiàn)箭頭沿圓轉(zhuǎn)動(dòng)實(shí)例代碼
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)箭頭沿圓轉(zhuǎn)動(dòng)實(shí)例代碼,需要的朋友可以參考下2017-09-09
Android自定義對(duì)話框的簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android自定義對(duì)話框的簡(jiǎn)單實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
Android listview的滑動(dòng)沖突解決方法
這篇文章主要介紹了Android listview的滑動(dòng)沖突解決方法的相關(guān)資料,需要的朋友可以參考下2017-02-02
native.js獲取手機(jī)硬件基本信息實(shí)例代碼android版
本文為大家分享了native.js獲取手機(jī)硬件基本信息實(shí)例代碼android版包括手機(jī)MAC地址,手機(jī)內(nèi)存大小,手機(jī)存儲(chǔ)空間大小,手機(jī)CPU信息等手機(jī)硬件基本信息2018-09-09

