Android Phone Virtual button development problem point

Source: Internet
Author: User

Recently did the mobile Phone virtual button development, and the Huawei Glorious virtual button similar, the effect is as follows (the bottom part of the screen):



Function Description: Mobile phone boot, "virtual button" auto-hide, when the bottom edge from the phone to swipe up, it automatically appears! Without action, 3 seconds will automatically disappear.

To implement this function, we need to solve these problems: (1) How to enable it to start when the phone is powered on, (2) How to be able to respond in any interface to the user needs "virtual button" intention, and pop up "virtual button", (3) How to detect the user from the bottom edge of the phone to swipe up the event; (4) How to simulate real home_key, Menu_key and Return_key events.

The following one solves these problems:

(1) How to get it started when the phone is powered on:

This is simple, defining a receiver whose action is: Android.intent.action.BOOT_COMPLETED so you can. However, you will find that at least a longer delay when the phone is powered on, we define the action to be able to receive!

(2) to be able to appear in any interface "virtual button", and can do some things, it is clear that we need a service, here my service is: Navbarservice. In all the interface can appear "virtual button", we can only use WindowManager, we all know can use WindowManager to achieve hover window, in fact, we "virtual button" is a hover view.

First get the real column of WindowManager by the following code: WindowManager WM;

WM = (WindowManager) mcontext.getsystemservice ("window");
After the layout of the view (LinearLayout holder;) to add it up on the right:

Wm.addview (holder, layoutparams);
Here LinearLayout holder, is to use a linearlayout to layout! There are three buttons in the layout: Return button; Home button, menu button.

Here we should note that the second parameter: layoutparams

            Windowmanager.layoutparams layoutparams = new Windowmanager.layoutparams (                    WindowManager.LayoutParams.MATCH_ PARENT,                    WindowManager.LayoutParams.MATCH_PARENT);            Layoutparams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;            Layoutparams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE                    | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;            Layoutparams.format = pixelformat.translucent;
(3) How to detect the user from the bottom edge of the mobile phone to slide up the event;

This is simple! is to add a simple view with Wm.addview, with the view set to all transparent! And the height only needs 15dip, the view is placed at the bottom of the screen. Use this view to detect the intention of the user to swipe up.

So finally formed the all transparent view and the above mentioned three buttons of the view (holder) to each other, switch time with WindowManager renove to remove the temporarily unwanted view can be, such as: Wm.removeview ( Holder);

(4) How to simulate real home_key, Menu_key and Return_key events.

Start with three button view Setonlongclicklistenerset and Setonclicklistener to listen for long press and short press events! The next step is how to simulate real-world keys, which are instrumentation

    public static void Home_longdown () {//LOG.I (TAG, "Home home_longdown systemclock.uptimemillis () =" +systemclock.       Uptimemillis ()); New Thread () {public void run () {try {keyevent ev = new KeyEvent (Systemclock.uptimemillis ()-10 0, Systemclock.uptimemillis (), Keyevent.action_down, Keyevent.keycode_home, 1,0, Keycharactermap.virtual_keyboard, 0 , Keyevent.flag_long_press | Keyevent.flag_from_system |                    Keyevent.flag_virtual_hard_key,inputdevice.source_keyboard);                   New Instrumentation (). Sendkeysync (EV);                    LOG.I (TAG, "Home_longdown");                Return                } catch (Exception e) {log.d (TAG, e.tostring ());    }}}.start ();//playsoundeffect (Android.view.SoundEffectConstants.CLICK);        } public static void Home_click () {//log.i (TAG, "Home Click");        Playsoundeffect (Android.view.SoundEffectConstants.CLICK);         New Thread () {   public void Run () {try {new instrumentation (). Sendkeydownupsync (Keyevent.keycode_ho                  ME);                    LOG.I (TAG, "Home");                Return                } catch (Exception e) {log.d (TAG, e.tostring ());    }}}.start (); }

The code above is the code that simulates Home-key long press and short press events! The other Menu-key and Return-key are the same.

Android Phone Virtual button development problem point

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.