提问人:Javier 提问时间:11/4/2023 更新时间:11/4/2023 访问量:14
我可以使用 ClassLoader 加载片段,同时使用 APK 的权限吗?
Can I use ClassLoader to load a fragment but also with the permissions from the APK?
问:
我有一个应用程序,我想从第二个应用程序(我将开发)动态加载一个片段,背后的想法是,如果应用程序不是“呼叫应用程序”,Google Play 不允许某些权限,例如默认呼叫应用程序,所以我的想法是创建一个新的 APK,然后用户必须从我的网站或其他地方下载 apk, 主应用程序不会有下载 apk 的逻辑(因为这可能会被标记为恶意软件),然后这个想法是使用 Classloader(我设法做到了)加载 Fragment,或使用像这样的库: https://github.com/Catherine22/ClassLoader 问题是假设我在第一个 APK 中没有互联网权限, 我在第二个 APK 中拥有互联网权限,如果我在主应用程序中加载“第二个 apk”片段,我将收到一个错误,因为在主应用程序中我没有互联网权限。
虽然我知道这看起来是一个很好的安全措施,但我只想知道是否有可能为我的用户提供更多功能和选项,Tasker 会做一些类似于使用旧版本伴侣控制 WIFI 的事情,它可能和我想要的不一样,但这给了我一个想法,如果他们想要的话,可以给我的用户提供更多功能。 这可能吗? 我使用了这个从SO使用应用程序内部的外部应用程序片段/活动的截取代码
var requiredClass: Class<*>? = null
val apkPath = packageManager.getApplicationInfo("com.test.composetest", 0).sourceDir
val dexTemp: File = getDir("temp_folder", 0)
val fullName = "com.test.composetest.TestFragment"
var isLoaded = true;
// Check if class loaded
try {
requiredClass = Class.forName(fullName);
} catch (e: ClassNotFoundException) {
isLoaded = false;
}
if (!isLoaded) {
val classLoader = DexClassLoader(
apkPath, dexTemp.absolutePath, null,
applicationContext.classLoader
)
requiredClass = classLoader.loadClass(fullName);
}
if (requiredClass != null) {
// Try to cast to required interface to ensure that it's can be cast
val holder = FragmentHolder::class.cast(requiredClass.newInstance())
if (null != holder) {
val fragment: Fragment = holder.getFragment();
if (null != fragment) {
val trans: FragmentTransaction =
supportFragmentManager.beginTransaction()
trans.add(R.id.fragmentPlace, fragment, "MyFragment").commit();
}
}
}
} catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace();
} catch (e: ClassNotFoundException) {
e.printStackTrace();
} catch (e: InstantiationException) {
e.printStackTrace();
} catch (e: IllegalAccessException) {
e.printStackTrace();
}
答: 暂无答案
上一个:链接首选项屏幕
下一个:片段中的导航问题。人造人
评论
ClassLoader
ClassLoader