Taking the android status bar as an example to learn how to insert key messages in the input system

Source: Internet
Author: User

From: http://www.w3c.com.cn/%E4%BB%A5android%E7%8A%B6%E6%80%81%E6%A0%8F%E8%99%9A%E6%8B%9F%E6%8C%89%E9%94% AE %E6%B6%88%E6%81%AF%E4%B8%BA%E4%BE%8B%E5%AD%A6%E4%B9%A0%E5%9C%A8input%E7%B3%BB%E7%BB%9F%E6%8F%92%E5%85%A5%E6%8C%89

Taking the android status bar as an example to learn how to insert key messages in the input system

Recently, we have been considering how to convert key messages not generated by the local hardware into hardware key messages for processing. This situation is common and necessary in practical applications. Starting from android4.0, The systembar of the tablet has several buttons at the bottom of the tablet, including "return", "menu", "Recent", "vol +", and "vol -. These are all software icons. Click this icon, and systemui converts a corresponding key message to post to the system, and windowsmanager then responds accordingly. Then how does systemui work?

I followed it carefully. The corresponding file in android4.2 SDK is frameworks \ base \ packages \ systemui \ SRC \ com \ Android \ systemui \ statusbar \ Policy \ keybuttonview. java, which provides the conversion of key messages. The key code is as follows:

    void sendEvent(int action, int flags) {        sendEvent(action, flags, SystemClock.uptimeMillis());    }    void sendEvent(int action, int flags, long when) {        final int repeatCount = (flags & KeyEvent.FLAG_LONG_PRESS) != 0 ? 1 : 0;        final KeyEvent ev = new KeyEvent(mDownTime, when, action, mCode, repeatCount,                0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0,                flags | KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY,                InputDevice.SOURCE_KEYBOARD);        InputManager.getInstance().injectInputEvent(ev,                InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);    }

We can see that the core is to first create a new message object, then forcibly insert a message through the injectinputevent method of the getinstance of the inputmanager object, and then dispatch the message.


In the application of the automotive terminal, key messages are generally processed by a single-chip microcomputer. After the single-chip microcomputer is processed, it uses a serial port to provide the master chip. Generally, the serial port drivers of the master chip are standard tty devices. In actual development, we generally do not intercept this message in the driver, because such serial port interconnection generally carries certain control messages, resolution required. The key message obtained through the serial port is already a definite value, but how can it be applied? Android has a complete set of processing for this input message. Therefore, we need to convert it into a hardware key message. Fortunately, Android has long considered this situation, so we can use injectinputevent to insert a message. Based on the above study, I also did some tests, which is indeed feasible. The added call test code is as follows:

                mDownTime = SystemClock.uptimeMillis();                mCode = 24;                sendEvent(KeyEvent.ACTION_DOWN, 0, mDownTime);

The SDK Source Code contains the essence. You can take a closer look at the code to learn a lot.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.