Android listen for all events in application
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 {
}
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.
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.
There is no OnEventListener interface. You will need to implement each one you want to listen.
You can do it this way:
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
}
We had the same problem, so we developed the Android HCI Extractor some months ago. It was made to track and monitor the user and system interaction events in multimodal applications (e.g. touch, keypresses, scroll, number of elements provided by the system, etc.)
It is very easy to integrate and to use. In the tutorials you can see that only a few lines of code are needed. Moreover, with this solution you do not have to implement all listeners; you only need to add code into the methods in the /android.hci.extractor/src/org/mmi/facades/YourFacade.java class.
Here the links: - Android HCI Extractor code: http://code.google.com/p/android-hci-extractor/ - MIM project (including tutorials about the tool integration and usage): http://www.catedrasaes.org/trac/wiki/MIM
I hope it helps you!!
There are some methods in the Activity class you can override that may help. I used this when I had to intercept all events for all view controls in order to reset an idle timeout countdown. It sounds like you have a similar wide-scoped need to handle all events similarly, so this might be just what you need:
dispatchTouchEvent()
dispatchKeyEvent()
Create a base activity class that all activities extend and add the following snippet to the base activity
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) { // If you only want the touch events
doYourAction(); //Your action for each touch event
}
return super.dispatchTouchEvent(ev);
}
精彩评论