来自 the docs 我们看到我们能够通过工厂方法提供 Activity 。
所以我正在研究一个解决方案,我可以在其中进行 Activity 并在其之上装饰功能。但我不拥有这些 Activity 。我只知道那里的名字。这就是我到目前为止所拥有的:
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
AppComponentFactory.instantiateApplication(classLoader, SomeoneElsesActivity.class.name)
我将从我自己的 Activity 中调用它来打开SomeElsesActivity.class。但我不清楚如何添加额外的功能。我会在 Activity 打开后生成一个像 Intent 服务这样的后台服务吗?
如果我能够使用包装器,有没有办法阻止其他人对我的 Activity 执行此操作?我不希望任何其他开发人员能够获取我的 Activity 并围绕它包装功能并再次调用它。
最佳答案
更新 2018-06-03:AppComponentFactory
在 P DP2 中工作。
from the docs we see we are able to provide an acitivity through factory method.
更重要的是,您可以通过 android:appComponentFactory
在 list 中注册您的 AppComponentFactory
。然后your process crashes, because AppComponentFactory
will not be working until the next developer preview .
i will be calling this from my own activity to open SomeoneElsesActivity.class
据我所知,这不是您使用AppComponentFactory
的方式。 AppComponentFactory
为框架提供回调以用于创建 Activity 实例。
and if i am able to use the wrapper is there a way to disable other people from doing this to my activity ?
不要运送库。欢迎包含您的库的应用程序的开发人员在他们的应用程序上下文中做任何他们想做的事情。
如果您指的是某人重新打包您的应用程序,他们无论如何都可以这样做 - 这只是使该工作的一方面变得更容易。原则上,您可以从 Activity 的零参数构造函数检查 Java 调用堆栈,尝试确定是否有人正在使用 AppComponentFactory
,然后采取措施。我不知道从长远来看这有多实用......特别是因为 AppComponentFactory
似乎不起作用。
关于android P 中的 android AppComponentFactory - 如何围绕外部 Activity 包装/装饰功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49231828/