我想使用隐藏的Android包管理器的方法installPackage
和回调类IPackageInstallObserver
。 我设法获取通过Java的反射问题的方法,但我在努力实现通过反射代理隐藏的观察者接口。
我尝试下面的方法 ,但它仍然没有奏效。 我想它是这样的:
// 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
实例不幸空-有人可以告诉我,我做错了什么? 我有系统权限和权限声明。