活动拦截器

发布于 2024-10-12 16:15:09 字数 270 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

木緿 2024-10-19 16:15:09

roboguice 1.1.1 版本包括对注入上下文的组件的一些基本事件支持。请参阅 http://code.google.com/p/roboguice/wiki/Events 了解更多信息。

例如:

@ContextScoped
public class MyObserver {
  void handleOnCreate(@Observes OnCreatedEvent e) {
    Log.i("MyTag", "onCreated");
  }
}

public class MyActivity extends RoboActivity {
  @Inject MyObserver observer;  // injecting the component here will cause auto-wiring of the handleOnCreate method in the component.

  protected void onCreate(Bundle state) {
    super.onCreate(state); /* observer.handleOnCreate() will be invoked here */
  }
}

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:

@ContextScoped
public class MyObserver {
  void handleOnCreate(@Observes OnCreatedEvent e) {
    Log.i("MyTag", "onCreated");
  }
}

public class MyActivity extends RoboActivity {
  @Inject MyObserver observer;  // injecting the component here will cause auto-wiring of the handleOnCreate method in the component.

  protected void onCreate(Bundle state) {
    super.onCreate(state); /* observer.handleOnCreate() will be invoked here */
  }
}
-残月青衣踏尘吟 2024-10-19 16:15:09

您可以将所有重复性工作委托给另一个类,该类将嵌入到您的其他活动中。这样,您就可以将重复工作限制为创建该对象并调用其 onCreate、onDestroy 方法。

class MyActivityDelegate {
    MyActivityDelegate(Activity a) {}

    public void onCreate(Bundle savedInstanceState) {}
    public void onDestroy() {}
}

class MyActivity extends ListActivity {
    MyActivityDelegate commonStuff;

    public MyActivity() {
        commonStuff = MyActivityDelegate(this);
    }

    public onCreate(Bundle savedInstanceState) {
        commonStuff.onCreate(savedInstanceState);
        // ...
    }
}

这可以最大限度地减少麻烦,并分解活动的所有常用方法和成员。另一种方法是对所有 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.

class MyActivityDelegate {
    MyActivityDelegate(Activity a) {}

    public void onCreate(Bundle savedInstanceState) {}
    public void onDestroy() {}
}

class MyActivity extends ListActivity {
    MyActivityDelegate commonStuff;

    public MyActivity() {
        commonStuff = MyActivityDelegate(this);
    }

    public onCreate(Bundle savedInstanceState) {
        commonStuff.onCreate(savedInstanceState);
        // ...
    }
}

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 :(

公布 2024-10-19 16:15:09

Take a look at http://code.google.com/p/android-method-interceptor/, it uses Java Proxies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文