具有系統簽名的APK實現APK靜默安裝
阿新 • • 發佈:2018-12-13
針對具有系統簽名許可權的APK(系統運用)要實現對其它第三方APK靜默安裝可用如下程式碼實現
1、manifest中許可權申請
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
2、實現方法
public void installSilent(Context context, String filePath) { if (filePath == null || filePath.length() == 0) { return ; } File file = new File(filePath); if (file == null || file.length() <= 0 || !file.exists() || !file.isFile()) { return ; } int installFlags = 0; Uri packageUri = Uri.fromFile(file);//file是要安裝的apk檔案 PackageManager pm = context.getPackageManager(); installFlags |= PackageManager.INSTALL_REPLACE_EXISTING; MyPackageInstallObserver observer = new MyPackageInstallObserver(); pm.installPackage(packageUri, observer, installFlags, null); }
3、安裝狀態回撥
private static class MyPackageInstallObserver implements IPackageInstallObserver { @Override public IBinder asBinder() { // TODO Auto-generated method stub return null; } @Override public void packageInstalled(String arg0, int arg1) throws RemoteException { // TODO Auto-generated method stub } }