简体   繁体   中英

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)?

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.

You cannot handle the touch events without making calls in java. The only way is to extend the component classes or customize the android SDK .jar which is a lengthy task

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM