活动拦截器
android中有什么方法可以拦截活动方法调用(只是标准的方法,例如“onStart.onCreate”)? 我有很多功能必须存在于我的应用程序中的每个活动中,并且(因为它使用不同类型的活动(列表、首选项))唯一的方法是为每个活动类创建自定义扩展,这糟糕 :(
PS 我用的是 roboguice,但是由于 Dalvik 不支持运行时代码生成,所以我想它没有多大帮助。
PSS 我想过使用 AspectJ,但是太麻烦了,因为它需要很多复杂性(ant 的 build.xml 和所有那些垃圾)
Is there any way in android to intercept activity method calls (just the standart ones, like "onStart. onCreate")?
I have a lot of functionality that must be present in every activity in my app, and (since it uses different types of activities (List, Preferences)) the only way to do it is to create my custom extensions for every activity class, which sucks :(
P.S. I use roboguice, but since Dalvik doesn't support code generation at runtime, I guess it doesn't help much.
P.S.S. I thought about using AspectJ, but it's too much of a hassle since it requires a lot of complications (ant's build.xml and all that junk)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
roboguice 1.1.1 版本包括对注入上下文的组件的一些基本事件支持。请参阅 http://code.google.com/p/roboguice/wiki/Events 了解更多信息。
例如:
The roboguice 1.1.1 release includes some basic event support for components injected into a context. See http://code.google.com/p/roboguice/wiki/Events for more info.
For Example:
您可以将所有重复性工作委托给另一个类,该类将嵌入到您的其他活动中。这样,您就可以将重复工作限制为创建该对象并调用其 onCreate、onDestroy 方法。
这可以最大限度地减少麻烦,并分解活动的所有常用方法和成员。另一种方法是对所有 API 的 XXXActivty 类进行子类化:(
You could delegate all the repetitive work to another class that would be embedded in your other activities. This way you limit the repetitive work to creating this object and calling its onCreate, onDestroy methods.
This minimalises the hassle and factorises all common methods and members of your activities. The other way to do it is to subclasse all the API's XXXActivty classes :(
看看 http://code.google.com/p/android-method-拦截器/,它使用Java代理。
Take a look at http://code.google.com/p/android-method-interceptor/, it uses Java Proxies.