Android的监听应用中的所有事件 [英] Android listen for all events in application

查看:273
本文介绍了Android的监听应用中的所有事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要听的电话上的所有用户活动 - 如 onTouch 的onClick onMenuItemClicked 等。
对于这一点,我已经延伸活动一个超类,所有活动从这个类扩展。

I want to listen for all user events on the phone - like onTouch, onClick, onMenuItemClicked etc. For this, I have made a super class which extends activity, and all activities extend from this class.

public class TopActivity extends Activity {

}


public class screen1 extends TopActivity {

}

有关监听事件我在这样的超类实现监听功能 - onTouch()的onClick() onMenuOpened()等,这些调用它具有code,当任何事件发生时,应运行相同的功能。

For listening to events I have implemented the listener functions in the super class like these - onTouch(), onClick(), onMenuOpened(), etc. All these call the same function which has the code that should run when any event occurs.

问题是,我将不得不实行每一个听众。有没有更好的方式来做到这一点?我只是想运行在同一块code时,曾经有一个在应用程序的任何用户事件。

The problem is that I would have to implement every listener. Is there a better way to do this? I just want to run the same piece of code when ever there is any user event in the application.

推荐答案

有没有OnEventListener接口。您将需要实现你想要听每一个。

There is no OnEventListener interface. You will need to implement each one you want to listen.

您可以这样来做:

public class TopActivity extends Activity implements OnClickListener, 
  OnTouchListener, OnKeyListener, OnLongClickListener /*etc*/ {

  public void onClick() {
    doSomething();
  }

  public boolean onKey(View v, int keyCode, KeyEvent event) {
    doSomething();
  }

  // etc
}

这篇关于Android的监听应用中的所有事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆