Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I handle touch screen events in Android Native code without going through Java*?

We would like to handle all touch inputs with Android Native code. We want to initialize the code from Android's onCreate() method, then have it take over all inputs. We have looked at native-activity in the sample, however some of the structs and methods it uses, we believe, are only available to all-native applications.

Once initialized, it should basically run on its own, processing touch events. It will call back to Java methods, which we have already figured out. The issue we are having is preventing Java from handling the methods, and how to have the native code handle touch inputs without having it called and the event passed through.

How can we set up this native code to handle all touch events, without having to go through Activity.onTouchEvent(MotionEvent event)?

like image 818
steve3280712 Avatar asked Feb 12 '14 07:02

steve3280712


People also ask

How do I get touchscreen event on android?

You can react to touch events in your custom views and your activities. Android supports multiple pointers, e.g. fingers which are interacting with the screen. The base class for touch support is the MotionEvent class which is passed to Views via the onTouchEvent() method. you override the onTouchEvent() method.

How are Android touch events delivered?

The touch event is passed in as a MotionEvent , which contains the x,y coordinates, time, type of event, and other information. The touch event is sent to the Window's superDispatchTouchEvent() . Window is an abstract class.

Which method you should override to control your touch action android?

To make sure that each view correctly receives the touch events intended for it, override the onInterceptTouchEvent() method. Refer to the following related resources: Input Events API Guide. Sensors Overview.

How does Android handle multi touch?

Multi-touch gesture happens when more then one finger touches the screen at the same time. Android allows us to detect these gestures. Android system generates the following touch events whenever multiple fingers touches the screen at the same time. For the first pointer that touches the screen.


1 Answers

As far as I know cannot do that.

As you said in comments, you can do it if your application runs in a native activity (sample native-activity shows how to start with it). At the end, you'll probably have something like:

int32_t handle_input(struct android_app* app, AInputEvent* event) {
    int32_t eventType = AInputEvent_getType(event);
    switch(eventType){
        case AINPUT_EVENT_TYPE_MOTION:
            switch(AInputEvent_getSource(event)){
                case AINPUT_SOURCE_TOUCHSCREEN:
                    int action = AKeyEvent_getAction(event) & AMOTION_EVENT_ACTION_MASK;
                    switch(action){
                        case AMOTION_EVENT_ACTION_DOWN:
                        break;
                        case AMOTION_EVENT_ACTION_UP:
                        break;
                        case AMOTION_EVENT_ACTION_MOVE:
                        break;
                    }
                break;
            } // end switch
        break;
        case AINPUT_EVENT_TYPE_KEY:
            // handle key input...
        break;
    } // end switch
}

If I'm not wrong, you cannot handle input in native code if your application runs in a standard Java Activity.

like image 93
Pau Guillamon Avatar answered Sep 21 '22 02:09

Pau Guillamon