Android 如何靜默安裝app

2021-07-16 03:02:08 字數 3931 閱讀 6304

1. 有提示的安裝(所有第三方應用都可以)

intent intent = new intent(intent.action_view);

intent.addflags(intent.flag_activity_new_task);

mcontext.startactivity(intent);

2. 靜默安裝:利用processbuilder

/**

* install slient

** @param filepath

* @return 0 means normal, 1 means file not exist, 2 means other exception error

*/public static int installsilent(string filepath)

string args = ;

processbuilder processbuilder = new processbuilder(args);

process process = null;

bufferedreader successresult = null;

bufferedreader errorresult = null;

stringbuilder successmsg = new stringbuilder();

stringbuilder errormsg = new stringbuilder();

int result;

try

while ((s = errorresult.readline()) != null)

} catch (ioexception e) catch (exception e) finally

if (errorresult != null)

} catch (ioexception e)

if (process != null)

}// todo should add memory is not enough here

if (successmsg.tostring().contains("success") || successmsg.tostring().contains("success")) else

log.d("test-test", "successmsg:" + successmsg + ", errormsg:" + errormsg);

return result;

}

3. 靜默安裝:利用runtime.getruntime().exec()

private static final string tag = "test-test";

private static final int time_out = 60 * 1000;

private static string sh_path = ;

public static boolean executeinstallcommand(string filepath)

successresult = new bufferedreader(new inputstreamreader(process.getinputstream()));

errorresult = new bufferedreader(new inputstreamreader(process.geterrorstream()));

os = new dataoutputstream(process.getoutputstream());

os.writebytes(command + "\n");

os.writebytes("echo \"rc:\" $?\n");

os.writebytes("exit\n");

os.flush();

string s;

while ((s = successresult.readline()) != null)

while ((s = errorresult.readline()) != null)

// handle a requested timeout, or just use waitfor() otherwise.

if (time_out > 0)

if (system.currenttimemillis() > finish)

}} else

// in order to consider this a success, we require to things: a) a proper exit value, and ...

if (process.exitvalue() != 0)

return true;

} catch (filenotfoundexception e) catch (ioexception e) catch (interruptedexception e) finally catch (ioexception e)

}try

if (errorresult != null)

} catch (ioexception e)

if (process != null) catch (illegalthreadstateexception e) }}

}private static process runwithenv(string command, string customenv) throws ioexception

}if (customenv != null)

}string arrayenv = null;

if (envlist.size() > 0)

}process process = runtime.getruntime().exec(command, arrayenv);

return process;

}/**

* check whether a process is still alive. we use this as a ***** way to implement timeouts.

*/private static boolean isprocessalive(process p) catch (illegalthreadstateexception e)

}/** get the su file path if it exist */

private static string getsupath()

}return "su";

}

4. 靜默安裝:利用反射呼叫api-packagemanager.installpackage()

public static void installsilentwithreflection(context context, string filepath)  );

method.setaccessible(true);

file apkfile = new file(filepath);

uri apkuri = uri.fromfile(apkfile);

method.invoke(packagemanager, new object

}, integer.valueof(2), "com.ali.babasecurity.yunos"});

= 2;

} catch (nosuchmethodexception e) catch (exception e)

}android_dependency.jar放到你工程的libs目錄,這個jar提供了與packagemanager反射呼叫相關的類的定義。

注意:靜默安裝還需要在你的androidmanifest.xml中新增許可權宣告。該許可權預設賦予系統應用,第三方應用即使宣告了,也拿不到該許可權!

Android 靜默安裝

熱修復現狀 sophix和tinker不支援新增四大元件,amigo由於阿里收購,不再維護並且支援gradle版本過低,導致好的技術喪失,技術的悲劇 靜默安裝核心步驟 public static boolean isrootpermission string result builder.tostr...

如何實現Android應用的靜默安裝

在很多情況下,我們需要在android系統上實現應用程式的靜默安裝,但很不幸的是,從android 1.5之後google就不再提供靜默安裝的介面了。下面介紹一種比較簡單的靜默安裝實現方法。原理 使用隱藏的系統api installpackage 呼叫installpackage的應用必須是系統應用...

Android 靜默安裝 後台安裝

android實現靜默安裝其實很簡單,今天在網上找資料找半天都說的很複雜,什麼需要系統安裝許可權 呼叫系統隱藏的api 需要系統環境下編譯 需要跟systemui同程序什麼的。我不知道他們真的實現了靜默安裝沒有,反正我按照他們的方式統統都失敗了。下面我來說說我的靜默安裝實現方式,親測效果跟豌豆莢一樣...