靜默安裝功能的實現
靜默安裝的實現比較簡單,但是有個前提:
你的應用必須有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
相關文章
- Android靜默安裝和靜默解除安裝Android
- 應用黑名單以及靜默安裝的實現
- Android 無需root實現apk的靜默安裝AndroidAPK
- rac靜默安裝
- dbca 靜默安裝
- Oracle 靜默安裝Oracle
- Oracle靜默安裝Oracle
- android靜默安裝apk已經成功實現AndroidAPK
- Android靜默安裝應用和靜默解除安裝應用Android
- PackageInstaller 5.0原始碼分析靜默安裝與靜默解除安裝Package原始碼
- 靜默安裝Azure CLI
- 靜默安裝ORACLE(文件)Oracle
- Android 流氓軟體靜默安裝是怎麼實現的?Android
- Oracle資料庫靜默安裝實踐Oracle資料庫
- oracle靜默安裝raw裝置Oracle
- 靜默安裝oracle時報錯Oracle
- ORACLE 11.2.0.4靜默安裝Oracle
- 靜默安裝oracle軟體Oracle
- Oracle靜默安裝(單機)Oracle
- 靜默安裝、建庫(轉)
- 靜默安裝ORACLE 軟體Oracle
- Oracle靜默安裝說明Oracle
- Android 靜默安裝/後臺安裝Android
- 【11g 單庫解除安裝、靜默安裝】實驗
- 【靜默】在RHEL 6.5上靜默安裝Oracle 18cOracle
- android apk靜默安裝和解除安裝AndroidAPK
- android 4.0.4系統下實現apk的靜默安裝和啟動AndroidAPK
- oracle 19C 靜默安裝Oracle
- Linux下靜默安裝OraceLinux
- 靜默安裝and手動建庫
- oracle 12c 靜默安裝Oracle
- Oracle 11g 靜默安裝Oracle
- 靜默安裝oracle10gOracle
- AIX 6.1 靜默安裝11.1.0.6AI
- oracle10g 靜默安裝Oracle
- Oracle 10g 靜默安裝Oracle 10g
- RHEL5 Oracle 11G R2 RAC 靜默安裝 (二)GI靜默安裝Oracle
- 11.2靜默安裝碰到的錯誤