android apk靜默安裝和解除安裝
轉自:http://blog.csdn.net/gchk125/article/details/8553805
靜默安裝:
在某個觸發點自動後臺安裝指定apk.沒有安裝介面.新軟體自動安裝,有舊版本的默默解除安裝,然後再安裝,並且在不受系統設定中的第三方軟體安裝開關的限制.
自己跟蹤記錄的原始碼流程
Filemanager中點選apk包之後傳送了一個intent出去
|
Android會去系統中的packagemanager中查詢哪個apk能響應這個操作.
找到PackageInstaller這個apk.檔案路勁位於packages\apps\PackageInstaller
|
系統會啟動這個Activity來進行安裝操作
本文件只貼出具體安裝的過程,捨棄ui操作,並加以簡單說明
|
|
|
|
好了.迴歸主題.靜默安裝的關鍵程式碼已經出來了.只有一行
pm.installPackage(mPackageURI,observer, installFlags(0), installerPackageName);
看原始碼中該函式說明必須要加上android.Manifest.permission#INSTALL_PACKAGES才行,此許可權只有system應用才能使用.所以此應用必須放入system/app下,才能保證其有效.方法就是反射
靜默安裝的第一種方法:
- package com.tnt.autoinstall;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.InputStream;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import android.net.Uri;
- import android.os.Bundle;
- import android.app.Activity;
- public class MainActivity extends Activity {
- private Uri packageUri;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- packageUri = getPackageUri();
- install();
- }
- private void install()
- {
- try {
- Class<?> pmService;
- Class<?> activityTherad;
- Method method;
- activityTherad = Class.forName("android.app.ActivityThread");
- Class<?> paramTypes[] = getParamTypes(activityTherad , "getPackageManager");
- method = activityTherad.getMethod("getPackageManager", paramTypes);
- Object PackageManagerService = method.invoke(activityTherad);
- pmService = PackageManagerService.getClass();
- Class<?> paramTypes1[] = getParamTypes(pmService , "installPackage");
- method = pmService.getMethod("installPackage", paramTypes1);
- method.invoke(PackageManagerService , packageUri , null , 0 , null);
- } catch (NoSuchMethodException e) {
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- }catch (ClassNotFoundException e1) {
- e1.printStackTrace();
- }
- }
- private Class<?>[] getParamTypes(Class<?> cls, String mName) {
- Class<?> cs[] = null;
- Method[] mtd = cls.getMethods();
- for (int i = 0; i < mtd.length; i++) {
- if (!mtd[i].getName().equals(mName)) {
- continue;
- }
- cs = mtd[i].getParameterTypes();
- }
- return cs;
- }
- private Uri getPackageUri()
- {
- File file = new File("/storage/sdcard0/download/1.apk");
- return Uri.fromFile(file);
- }
- }
靜默安裝的第二種方法:
通過adb命令的方式來處理,程式碼如下,把上面的install函式替換下面程式碼
- private boolean install()
- {
- String[] args = {"pm", "install", "-r", packageUri.getPath()};
- String result = null;
- ProcessBuilder processBuilder = new ProcessBuilder(args);;
- Process process = null;
- InputStream errIs = null;
- InputStream inIs = null;
- try{
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- int read = -1;
- process = processBuilder.start();
- errIs = process.getErrorStream();
- while((read = errIs.read()) != -1){
- baos.write(read);
- }
- baos.write('\n');
- inIs = process.getInputStream();
- while((read = inIs.read()) != -1){
- baos.write(read);
- }
- byte[] data = baos.toByteArray();
- result = new String(data);
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- try{
- if(errIs != null){
- errIs.close();
- }
- if(inIs != null){
- inIs.close();
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- if(process != null){
- process.destroy();
- }
- }
- if(result != null && (result.endsWith("Success")||result.endsWith("Success\n")))
- {
- return true;
- }
- return false;
- }
下面看下解除安裝過程
同樣的.需要增加一個許可權android:name="android.permission.DELETE_PACKAGES".放入system/app中.
解除安裝方式一:adb shell pm方式
- private void uninstall2() {
- String[] args = { "pm", "uninstall", "com.popcap.pvzthird" };
- String result = null;
- ProcessBuilder processBuilder = new ProcessBuilder(args);
- ;
- Process process = null;
- InputStream errIs = null;
- InputStream inIs = null;
- try {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- int read = -1;
- process = processBuilder.start();
- errIs = process.getErrorStream();
- while ((read = errIs.read()) != -1) {
- baos.write(read);
- }
- baos.write('\n');
- inIs = process.getInputStream();
- while ((read = inIs.read()) != -1) {
- baos.write(read);
- }
- byte[] data = baos.toByteArray();
- result = new String(data);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (errIs != null) {
- errIs.close();
- }
- if (inIs != null) {
- inIs.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- if (process != null) {
- process.destroy();
- }
- }
- }
解除安裝方式二:反射呼叫系統函式
- Class<?> pmService;
- Class<?> activityTherad;
- Method method;
- activityTherad = Class.forName("android.app.ActivityThread");
- Class<?> paramTypes[] = getParamTypes(activityTherad,
- "getPackageManager");
- method = activityTherad.getMethod("getPackageManager", paramTypes);
- Object PackageManagerService = method.invoke(activityTherad);
- pmService = PackageManagerService.getClass();
- Class<?> paramTypes1[] = getParamTypes(pmService, "deletePackage");
- method = pmService.getMethod("deletePackage", paramTypes1);
- method.invoke(PackageManagerService, "com.popcap.pvzthird", null, 0);
相關文章
- Android靜默安裝和靜默解除安裝Android
- Android靜默安裝應用和靜默解除安裝應用Android
- PackageInstaller 5.0原始碼分析靜默安裝與靜默解除安裝Package原始碼
- android靜默安裝apk已經成功實現AndroidAPK
- Android 靜默安裝/後臺安裝Android
- android 程式碼安裝和解除安裝apkAndroidAPK
- Android 無需root實現apk的靜默安裝AndroidAPK
- 【11g 單庫解除安裝、靜默安裝】實驗
- oracle 11g rac 靜默解除安裝Oracle
- android 4.0.4系統下實現apk的靜默安裝和啟動AndroidAPK
- rac靜默安裝
- dbca 靜默安裝
- Oracle 靜默安裝Oracle
- Oracle靜默安裝Oracle
- 靜默安裝Azure CLI
- 靜默安裝ORACLE(文件)Oracle
- oracle靜默安裝raw裝置Oracle
- 關於靜默安裝和刪除
- Android程式碼實現APK檔案的安裝與解除安裝AndroidAPK
- Oracle 19C的下載和安裝部署(圖形安裝和靜默安裝)Oracle
- 靜默安裝oracle時報錯Oracle
- ORACLE 11.2.0.4靜默安裝Oracle
- 靜默安裝oracle軟體Oracle
- Oracle靜默安裝(單機)Oracle
- 靜默安裝、建庫(轉)
- 靜默安裝ORACLE 軟體Oracle
- Oracle靜默安裝說明Oracle
- oracle安裝:使用響應檔案靜默安裝Oracle
- Ubuntu解除安裝和安裝Ubuntu
- oracle客戶端無人值守安裝 靜默安裝Oracle客戶端
- 【DBCA -SILENT】靜默安裝之rac資料庫安裝資料庫
- oracle 19C 靜默安裝Oracle
- Linux下靜默安裝OraceLinux
- 靜默安裝and手動建庫
- oracle 12c 靜默安裝Oracle
- Oracle 11g 靜默安裝Oracle
- 靜默安裝功能的實現
- 靜默安裝oracle10gOracle