具有系统签名的APK实现APK静默安装

针对具有系统签名权限的APK(系统运用)要实现对其它第三方APK静默安装可用如下代码实现

1、manifest中权限申请

   

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
    }
}

 

你可能感兴趣的:(android,java)