- 系統簽名
- 須要放到 /system/app裏做爲系統app
- 機頂盒開發,系統開發,車機開發,智能設備開發。
1.1. 在清單文件 AndroidManifest.xml 添加 android.uid.system 聲明爲系統應用。
1.2. 權限java<uses-permission android:name="android.permission.INSTALL_PACKAGES" /> <uses-permission android:name="android.permission.DELETE_PACKAGES" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> 再添加讀寫權限這個能夠實現安裝時讀取路徑 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />複製代碼
1.3 總配置步驟圖
android
- 以前公司開發時機器是api 17 ,獲取了系統裏pm的代碼經過aidl實現加反射實現 這個方法也是比較正規的作法
- 去大家系統的源碼裏找到android.content.pm把裏面的代碼拷拷出來看圖這幾個就夠了。
代碼段api
安裝 public void installApp(final String path, final String packageNames){ File apkFile = new File(path); /* 當前app沒法訪問外置sd卡文件,exists()會爲false因此直接拋異常最好 if(!apkFile.exists()){ sendBroadcastMsg(packageNames,false,"路徑錯誤"); return; }*/ try { Class<?> clazz = Class.forName("android.os.ServiceManager"); Method method_getService = clazz.getMethod("getService", String.class); IBinder bind = (IBinder) method_getService.invoke(null, "package"); IPackageManager iPm = IPackageManager.Stub.asInterface(bind); iPm.installPackage(Uri.fromFile(apkFile), new IPackageInstallObserver.Stub(){ @Override public void packageInstalled(String packageName, int returnCode) throws RemoteException { } }, 2, apkFile.getName()); } catch (Exception e) { sendBroadcastMsg(packageNames,false,"安裝異常"); e.printStackTrace(); } } 卸載 public void uninstallApp(String packageName){ try { Class<?> clazz = Class.forName("android.os.ServiceManager"); Method method_getService = clazz.getMethod("getService",String.class); IBinder bind = (IBinder) method_getService.invoke(null, "package"); IPackageManager iPm = IPackageManager.Stub.asInterface(bind); iPm.deletePackageAsUser(packageName,null,0,2); System.out.println("=================>>卸載成功"); } catch (Exception e) { e.printStackTrace(); System.out.println("=================>>卸載失敗"); } }複製代碼
1. rxjava版 安裝若是不要rxjava能夠new Thread但要記住線程裏不要有刷新ui操做
private void install(final String packageName, final String filePath) {
Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> e) throws Exception {
File file = new File(filePath);
if (filePath == null || filePath.length() == 0 || file == null) {
e.onNext(0);
return;
}
String[] args = { "pm", "install", "-r", filePath };
ProcessBuilder processBuilder = new ProcessBuilder(args);
Process process = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = new StringBuilder();
StringBuilder errorMsg = new StringBuilder();
try {
process = processBuilder.start();
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 (IOException e1) {
e1.printStackTrace();
} finally {
try {
if (successResult != null) {
successResult.close();
}
if (errorResult != null) {
errorResult.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
if (process != null) {
process.destroy();
}
}
if (successMsg.toString().contains("Success") || successMsg.toString().contains("success")) {
e.onNext(2);
} else {
e.onNext(1);
}
}
}).subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Integer>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Integer value) {
if (value==2) {
//安裝成功
} else {
//安裝錯誤
}
}
@Override
public void onError(Throwable e) {
//安裝錯誤
}
@Override
public void onComplete() {
}
});
}
2.卸載
private void uninstall(String packageName) {
try {
PackageManager pm = context.getPackageManager();
Method[] methods = pm!=null?pm.getClass().getDeclaredMethods():null;
Method mDel = null;
if (methods != null && methods.length>0) {
for (Method method : methods) {
if (method.getName().toString().equals("deletePackage")) {
mDel = method;
break;
}
}
}
if (mDel != null) {
mDel.setAccessible(true);
mDel.invoke(pm,packageName,null,0);
}
} catch (Exception e) {
e.printStackTrace();
}
}複製代碼
- platform.pk8
- platform.x509.pem
- signapk.jar
- 下面是個人工具圖
- 方法一若是使用時若是缺乏類就把缺乏的類複製進去,若是參數不一樣記得隨機應變,通常原生系統是這 樣,通常開發不會改動太大pm.2 . 方法二是比較好的方便快捷並且兼容性好點。