靜默安裝功能的實現
靜默安裝的實現比較簡單,但是有個前提:
你的應用必須有system許可權。
所以分為兩步:
1. 如何獲取system許可權。
①. 修改androidmanifest.xml , android:sharedUserId="android.uid.system"
②. 修改android.mk ,增加LOCAL_CERTIFICATE := platform
③. 編譯即可,這樣你的app就有了system許可權。
2. 如何靜默安裝。
在這裡我選擇的是用Runtime.getRuntime().exec("pm install -r -s " );的方法。
具體實現如下,可以呼叫這個方法來安裝/system/installapp/下以1.apk 2.apk 命名的apk,並選擇是否安裝到SD卡。
- private int installapk_internel(int apkid, boolean sd) {
- ByteArrayOutputStream byteary = new ByteArrayOutputStream();
- ByteArrayOutputStream errbyteary = new ByteArrayOutputStream();
- InputStream input = null;
- InputStream errin = null;
- Process proc = null;
- int read = -1;
- int readerr= -1;
- String errresult= null;
- String result = null;
- try {
- if (sd == true) {
- proc = Runtime.getRuntime().exec(
- "pm install -r -s " + APK_DIR + Integer.toString(apkid)
- + ".apk");
- } else {
- proc = Runtime.getRuntime().exec(
- "pm install -r -f " + APK_DIR + Integer.toString(apkid)
- + ".apk");
- }
- int exitVal = proc.waitFor();
- // wait for the proc to complete
- input = proc.getInputStream();
- errin = proc.getErrorStream();
- while ((readerr = errin.read()) != -1) {
- errbyteary.write(readerr);
- }
- // byteary.write('\n');
- while ((read = input.read()) != -1) {
- byteary.write(read);
- }
- byte data[] = byteary.toByteArray();
- byte dataerr[] = errbyteary.toByteArray();
- result = new String(data);
- errresult =new String(dataerr);
- Log.d(TAG, "INSTALL done ! id= " + apkid + " result code = "
- + exitVal);
- Log.d(TAG,"INSTALL DONE ,err = "+errresult);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- Log.d(TAG,"INSTALL FAILD!");
- e.printStackTrace();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- if (input != null) {
- try {
- input.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (errin != null) {
- try {
- errin.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (proc != null) {
- proc.destroy();
- }
- Log.d(TAG, " install complete, result = " + result);
- }
- if (result != null&&errresult != null) {
- return handle_install_result(result,errresult);
- } else {
- return -3;
- }
- }
- private static final String APK_DIR = "/system/installapp/";
- private static final int INSTALL_APK_SUCESS = 0;
- private static final int INSTALL_APK_FAILED_SD_CARD_NOT_PRESENT = -1;
- private static final int INSTALL_APK_FAILED_INTENER_MEMORY_FULL = -2;
- private static final int INSTALL_APK_FAILED_OTHER = -3;
- private static final String INSTALL_RESULT_SUCCESS = "Success";
- private static final String INSTALL_RESULT_SD_NOT_PRESENT = "Failure [INSTALL_FAILED_MEDIA_UNAVAILABLE]";
- private int handle_install_result(String result,String errresult) {
- if (result.contains(INSTALL_RESULT_SUCCESS)) {
- return INSTALL_APK_SUCESS;
- } else if (errresult.contains(INSTALL_RESULT_SD_NOT_PRESENT)) {
- return INSTALL_APK_FAILED_SD_CARD_NOT_PRESENT;
- } else {
- return INSTALL_APK_FAILED_OTHER;
- }
- }
- int ret = installapk_internel(current_id, true);
- if (ret == INSTALL_APK_FAILED_SD_CARD_NOT_PRESENT) {
- // then we try to install apks to internel memory.
- Log.d(TAG,
- "sdcard not present, we try to install apk to internel flash .");
- ret = installapk_internel(current_id, false);
- Log.d(TAG,"ret= "+ret);
- if (ret != INSTALL_APK_SUCESS) {
- Log.d(TAG, "install faild again.");
- // WriteInstall_FAILD_FlagtoFile(current_id);
- }
- }
總的來說,靜默安裝還是比較簡單的,與大家分享下,共同進步。
http://blog.csdn.net/shadow_dance/article/details/8024049
相關文章
- 應用黑名單以及靜默安裝的實現
- Oracle靜默安裝Oracle
- rac靜默安裝
- Android 流氓軟體靜默安裝是怎麼實現的?Android
- Linux下靜默安裝OraceLinux
- 【11g 單庫解除安裝、靜默安裝】實驗
- 【靜默】在RHEL 6.5上靜默安裝Oracle 18cOracle
- AnolisOS 7.9 靜默安裝Oacle 11.2.0.4
- centos 7.4靜默安裝oracle 19.3CentOSOracle
- oracle 19C 靜默安裝Oracle
- 靜默安裝oracle時報錯Oracle
- 靜默安裝and手動建庫
- Windows 7平臺靜默安裝11.2.0.4軟體及靜默建庫Windows
- Oracle Linux 7.1 靜默安裝Weblogic 12.2.1.3OracleLinuxWeb
- 靜默方式安裝11gR2
- 靜默安裝19C RAC的指令碼指令碼
- win10如何禁止靜默執行_win10禁止靜默安裝方法Win10
- CentOS 7.5靜默安裝Oracle 11gCentOSOracle
- RedHat上靜默安裝Oracle11gRedhatOracle
- CentOS 7.2靜默安裝Oracle11gCentOSOracle
- redhat7.2靜默安裝Oracle11.2.0.4RedhatOracle
- 靜默方式安裝、升級oracle(一): 安裝oracle軟體Oracle
- alertmanager實現告警抑制及靜默
- windows10系統禁止靜默安裝的方法Windows
- Oracle 19C的下載和安裝部署(圖形安裝和靜默安裝)Oracle
- 靜默安裝Oracle資料庫11gOracle資料庫
- 靜默安裝Oracle11g資料庫Oracle資料庫
- 靜默方式安裝oracle 11g 完整攻略Oracle
- oracle11g客戶端靜默安裝Oracle客戶端
- RedHat 7 靜默安裝Oracle11g的補充RedhatOracle
- Oracle Linux 7.1 靜默安裝Oracle 18c RACOracleLinux
- linux7 靜默安裝 11GR2 RACLinux
- 19c(19.3) 單機資料庫靜默安裝資料庫
- 靜默方式安裝、升級oracle(二): 建立資料庫Oracle資料庫
- Linux下利用指令碼靜默安裝Oracle11GLinux指令碼Oracle
- GoldenGate 12.3 MA架構介紹系列(5) - 靜默安裝Go架構
- oracle11g在linux7的靜默安裝指令碼OracleLinux指令碼
- 【配置安裝】靜默安裝Oracle資料庫軟體12c&19cOracle資料庫
- 突破技術限制,實現Web端靜默列印Web