各種以android硬件平臺為基礎(chǔ)的【公示屏】、【廣告屏】等等,雖然很少有升級,但是不可避免的會遇到,而此類APP的使用場景,一般沒人會去幫助你版本更新,點(diǎn)擊安裝,故而需要:靜默安裝。
創(chuàng)新互聯(lián)公司基于成都重慶香港及美國等地區(qū)分布式IDC機(jī)房數(shù)據(jù)中心構(gòu)建的電信大帶寬,聯(lián)通大帶寬,移動大帶寬,多線BGP大帶寬租用,是為眾多客戶提供專業(yè)綿陽主機(jī)托管報價,主機(jī)托管價格性價比高,為金融證券行業(yè)服務(wù)器托管,ai人工智能服務(wù)器托管提供bgp線路100M獨(dú)享,G口帶寬及機(jī)柜租用的專業(yè)成都idc公司。
1、確認(rèn)安裝包是否存在,并可讀寫
2、隱示啟動:action和data的schema來控制彈出安裝工具類APP,然后點(diǎn)擊安裝...
3、升級完:BootReceiver 監(jiān)聽到Intent.ACTION_PACKAGE_REPLACED,然后自啟動
靜默安裝apk接口,無需開放root,也無需system權(quán)限。
原理
靜默安裝、卸載的原理就是利用pm install命令來安裝apk,pm uninstall 來卸載apk.
智能安裝是利用android系統(tǒng)提供的無障礙服務(wù)AccessibilityService,來模擬用戶點(diǎn)擊,從而自動安裝.
//靜默安裝
private?void?installSlient()?{
String?cmd?=?"pm?install?-r?/mnt/sdcard/test.apk";
Process?process?=?null;
DataOutputStream?os?=?null;
BufferedReader?successResult?=?null;
BufferedReader?errorResult?=?null;
StringBuilder?successMsg?=?null;
StringBuilder?errorMsg?=?null;
try?{
//靜默安裝需要root權(quán)限
process?=?Runtime.getRuntime().exec("su");
os?=?new?DataOutputStream(process.getOutputStream());
os.write(cmd.getBytes());
os.writeBytes("\n");
os.writeBytes("exit\n");
os.flush();
//執(zhí)行命令
process.waitFor();
//獲取返回結(jié)果
successMsg?=?new?StringBuilder();
errorMsg?=?new?StringBuilder();
successResult?=?new?BufferedReader(new?InputStreamReader(process.getInputStream()));
errorResult?=?new?BufferedReader(new?InputStreamReader(process.getErrorStream()));
String?s;
while?((s?=?successResult.readLine())?!=?null)?{
successMsg.append(s);
}
while?((s?=?errorResult.readLine())?!=?null)?{
errorMsg.append(s);
}
}?catch?(Exception?e)?{
e.printStackTrace();
}?finally?{
try?{
if?(os?!=?null)?{
os.close();
}
if?(process?!=?null)?{
process.destroy();
}
if?(successResult?!=?null)?{
successResult.close();
}
if?(errorResult?!=?null)?{
errorResult.close();
}
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
//顯示結(jié)果
tvTest.setText("成功消息:"?+?successMsg.toString()?+?"\n"?+?"錯誤消息:?"?+?errorMsg.toString());
}
一.轟隆一聲靂響,我閃亮登場。
本篇基于已有系統(tǒng)證書(從Android設(shè)備廠家獲得)的情況下實(shí)現(xiàn)靜默安裝與靜默卸載,可分為三部分講解:將apk內(nèi)置為系統(tǒng)應(yīng)用,apk靜默安裝與apk靜默卸載。
1.將apk內(nèi)置為系統(tǒng)應(yīng)用。內(nèi)置的方法有共性,也有區(qū)別?;A(chǔ)操作是共性,區(qū)別就在于Android4.4以上版本與Android4.4以下版本。
2.apk靜默安裝。
3.apk靜默卸載。
二.若您覺得本文對您有幫助,記得點(diǎn)個關(guān)注喲~
1.靜默卸載實(shí)現(xiàn):
/**
* 靜默卸載app
*
* @param context
* @param packageName app的包名
* @throws IOException
* @throws InterruptedException
*/
public static void uninstallApp(Context context, String packageName) throws IOException, InterruptedException {
? ? ListPackageInfo packageInfos = context.getPackageManager().getInstalledPackages(PackageManager.GET_ACTIVITIES);
? ? for (PackageInfo packageInfo1 : packageInfos) {
? ? ? ? if (packageName.equals(packageInfo1.packageName)) {
? ? ? ? ? ? String suPath = "/system/xbin/su";
? ? ? ? ? ? File file = new File(suPath);
? ? ? ? ? ? if (!file.exists()) {
? ? ? ? ? ? ? ? suPath = "/system/bin/su";
? ? ? ? ? ? }
? ? ? ? ? ? Process process = Runtime.getRuntime().exec(suPath);
? ? ? ? ? ? String cmd = "pm uninstall " + packageName + "\n" + "exit\n";
? ? ? ? ? ? process.getOutputStream().write(cmd.getBytes());
? ? ? ? ? ? process.waitFor();
? ? ? ? ? ? break;
? ? ? ? }
? ? }
}
2.靜默安裝實(shí)現(xiàn):
/**
* 靜默安裝app
*
* @param filePath
* @throws IOException
* @throws InterruptedException
*/
public static void installApp(String filePath) throws IOException, InterruptedException {
? ? String suPath = "/system/xbin/su";
? ? File file = new File(suPath);
? ? if (!file.exists()) {
? ? ? ? suPath = "/system/bin/su";
? ? }
? ? Process process = Runtime.getRuntime().exec(suPath);
? ? String cmd = "pm install -r " + filePath + "\n" + "exit\n";
? ? process.getOutputStream().write(cmd.getBytes());
? ? process.waitFor();
}
最后加上重啟命令:
/**
* 重啟系統(tǒng)
*
* @return
*/
public static boolean reboot() {
? ? try {
? ? ? ? String suPath = "/system/xbin/su";
? ? ? ? File file = new File(suPath);
? ? ? ? if (!file.exists()) {
? ? ? ? ? ? suPath = "/system/bin/su";
? ? ? ? }
? ? ? ? Process process = Runtime.getRuntime().exec(suPath);
? ? ? ? String cmd = "reboot\nexit\n";
? ? ? ? process.getOutputStream().write(cmd.getBytes());
? ? ? ? return true;
? ? } catch (IOException error) {
? ? ? ? return false;
? ? }
}
注意卸載和安裝需要在子線程中執(zhí)行;如果單純關(guān)機(jī)則用“reboot -p”命令。
個人了解到的靜默安裝的方式有以下4種:
我看了一些第三方的應(yīng)用市場,一般在設(shè)置下都會有前兩種靜默安裝的方式可供選擇,而后兩種靜默安裝的方式主要是廠商自己的應(yīng)用市場使用。
如果在7.0的系統(tǒng)上使用第三種靜默安裝的方式會出現(xiàn)以下錯誤:
參考:
Android7.0的靜默安裝失敗問題研究
Android N 靜默安裝和卸載
主要步驟如下:
我試了以上兩篇文章的介紹的方法,還是失敗,提示Failure [null],不知道怎么破了,可能是廠商的定制問題吧。。。還在思考中。。。
當(dāng)前文章:靜默安裝android,靜默安裝參數(shù)
本文地址:http://chinadenli.net/article0/dsshjio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、面包屑導(dǎo)航、域名注冊、商城網(wǎng)站、網(wǎng)站設(shè)計公司、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)