Android Question Physical Buttons in Android 5+

labcold

Member
Licensed User
Longtime User
I have developed a large (2K+ lines in Main and 6 service modules) app which runs on a smart watch under Android 5.1. Its part of a 2,500 active users system running through MQTT and all works really well including the servers and API written in B4J - so a HUGE thank you for your AWESOME software and libraries!

One of the trickiest bits was getting the physical buttons to be a part of my app not launching other apps, which was solved using the KIOSK approach and simple Java to trap the key presses and stop them being forwarded (extract of the code below - the full code times the presses for long and short events as well as cancels plus a myriad of other non relevant things). The watch has 3 buttons, 2 respond with key-presses and the other is the HOME button. All this works fine once the app is locked / screen pinned.
B4X:
Sub keyrelease(kv As Int)             'ignore
    Log("rels -> " & kv)
End Sub

Sub keypush(kv As Int)                 'ignore
    Log("push -> " & kv)
End Sub

#If JAVA
import android.view.*;

public boolean _onkeydown (int keyCode, android.view.KeyEvent event){
    //BA.Log("Down = " + keyCode);

    if (processBA.subExists("keypush")) {
        processBA.raiseEvent(null,"keypush",keyCode);
        return true;
    }   
    return true;
}

public boolean _onkeyup (int keyCode, android.view.KeyEvent event){
    //BA.Log("Up = " + keyCode);
    if (processBA.subExists("keyrelease")) {
        processBA.raiseEvent(null,"keyrelease",keyCode);
        return true;
    }   
    return true;
}
#End IF
#End Region

I am now porting to a different watch which has 4 buttons. One is HOME as above, one responds exactly the same as in the previous watch returning a key code of 4, the other two however NEVER return a keypress event - either from Java or Activity_KeyPress - they DO launch two apps, a heart monitor and compass app. If I lock the app / screen pinned then they are blocked and I get the toast about screen pining. BUT I cannot get the keypress event into my app.

I tried looking at the logs and found these events in there:
Calling a method in the system process without a qualified user: android.app.ContextImpl.startActivity:1349 android.app.ContextImpl.startActivity:1338 com.android.internal.policy.impl.PhoneWindowManager.interceptKeyBeforeDispatching:2946 com.android.server.wm.InputMonitor.interceptKeyBeforeDispatching:574 com.android.server.input.InputManagerService.interceptKeyBeforeDispatching:1547
START u0 {flg=0x10000000 cmp=com.jxtek.compass/.MainActivity} from uid 1000 from pid 727 on display 0

ACT-NEW_INTENT handled : 0 / NewIntentData{intents=[Intent { flg=0x10800000 cmp=com.jxtek.compass/.MainActivity }] token=android.os.BinderProxy@3584903d}


Calling a method in the system process without a qualified user: android.app.ContextImpl.startActivity:1349 android.app.ContextImpl.startActivity:1338 com.android.internal.policy.impl.PhoneWindowManager.interceptKeyBeforeDispatching:2964 com.android.server.wm.InputMonitor.interceptKeyBeforeDispatching:574 com.android.server.input.InputManagerService.interceptKeyBeforeDispatching:1547
START u0 {flg=0x10000000 cmp=com.jx.heart/.activity.HeartActivity} from uid 1000 from pid 727 on display 0

I am struggling trying to find a way to get these key presses into my app. All I need is to know they have been pressed and ideally check if they are held for a period. I have searched through and tried all the relevant examples I can find on the forum. Do you have any suggestions?

Many thanks
Harry
 

labcold

Member
Licensed User
Longtime User
Hi Erel, thank you for taking time to look at this.
Yes I understand the HOME button and use and it works within my application as expected as I use the Kiosk Pinning.

My current device has TWO additional physical buttons which return key values of 4 & 52 respectively.

The problem I have is that on my new device there are four buttons on the device. One is HOME which works as normally, One which returns key value 4.
BUT TWO of them:
1) I can trap (well stop the effects of trying to launch an app when the screen is pinned) using Kiosk approach
2) un-trapped do launch two separate applications in the system
3) do not produce any key press events either from activity_keypress or in java

They obviously cause an event to occur and launch an application based on their input so why arent the key presses visible?
I did wonder if they were like the volume buttons but I couldnt get anything from trapping those events either and the device doesnt use them for that anyway.
 
Upvote 0

labcold

Member
Licensed User
Longtime User
OK Thank you Erel
Such a shame that there isn't a better interface in Android for dealing with external buttons, it appears very convoluted when you think it would be easy!! One would have hoped that buttons would arrive in the same manner as touch button events or extended keyboard chars. Why some do and some don't seems accidental rather than planned.
I'm seriously considering using BT headsets with buttons to get around the issue {that being my client demands a physical button press for certain events not a touch event}
Anyway thanks again for your help and superb software which brought this project to reality in three months rather than 12!
 
Upvote 0
Top