很多第三方库都需要利用到Context,如何无侵入获取全局Context?
利用ContentProvider获取Context
这是当下很多流行的第三方库,包括LeakCanary、Picasso都是构建一个ContentProvider。
例如 LeakCanary中:
internal class AppWatcherInstaller : ContentProvider() {
override fun onCreate(): Boolean {
SharkLog.logger = DefaultCanaryLog()
// 获取到Context
val application = context!!.applicationContext as Application
InternalAppWatcher.install(application)
return true
}
}
优点是无侵入式,缺点是会拖慢app启动速度。
构建ActivityThread获取Context
通过构建ActivityThread仿造一个app,这是从genymobile的scrcpy学习到的新思路。
public static void fillAppInfo() {
try {
// ActivityThread activityThread = new Acti