我正在尝试使用隐藏的Android软件包管理器的方法installPackage
及其回调类IPackageInstallObserver
。我设法通过Java的反射来检索有问题的方法,但是却在通过反射代理来实现隐藏的观察者接口。
我尝试了following approach,但还是没有用。我这样尝试过:
// I declared the same interface signature locally
interface IPackageInstallObserver {
fun packageInstalled(packageName: String?, returnCode: Int)
}
// get the classes and method via reflection
val cPackageManager = Class.forName("android.content.pm.PackageManager")
val cPackageInstallObserver = Class.forName("android.content.pm.IPackageInstallObserver")
installPackage = cPackageManager.getMethod("installPackage", Uri::class.java, cPackageInstallObserver, Integer.TYPE, String::class.java)
INSTALL_REPLACE_EXISTING = cPackageManager.getField("INSTALL_REPLACE_EXISTING").getInt(null)
// create the observer
val installObserver = Proxy.newProxyInstance(
CustomPackageManager::class.java.classLoader,
arrayOf<Class<*>>(IPackageInstallObserver::class.java), // local definition
InstallObserverInvocationHandler(listener)
) as IPackageInstallObserver // local definition
// the invocation handler
class InstallObserverInvocationHandler(private val listener: PackageListener): InvocationHandler {
override fun invoke(proxy: Any?, method: Method?, args: Array<out Any>?): Any? {
if (method?.name == "packageInstalled" && method.parameterTypes.size == 2 &&
method.parameterTypes[0] == String::class.java &&
method.parameterTypes[1] == Integer.TYPE) {
/* custom implementation */
}
return null
}
}
// how I use the installPackage method
installPackage.invoke(context.packageManager, apkUri, installObserver, INSTALL_REPLACE_EXISTING, INSTALLER_NAME)
但是,我的installObserver
实例不幸地为空-有人可以告诉我我做错了什么吗?我确实拥有系统特权和必需的权限。