Android - Intercept Hardware KeyPress (PTT Button) when app is in background
Asked Answered
S

1

5

I'm developing an Android app that intercepts the pressing hardware buttons and makes REST calls to their pressure. The button that I have to intercept is the Push to talk (PTT) button, so not a regular button, such as power button or volume button.

When the application runs in the foreground I use the method onKeyDown (int keyCode, KeyEvent event). The PTT button, as the identifier, has the number 27 and then inside the method I wrote the following lines of code:

if (keyCode == 27) {// I pressed the PTT button}

All this works perfectly.

But now I send the application in the background (with the function moveTaskToBack(true);) and when I press the PTT button I would intercept it.

I am aware of BroadcastReceiver, of IntentFilter and Service, however, these allow you to intercept the limited intent actions (such intent.action.SCREEN_OFF or others normal actions), among which are not able to find the pressure of the PTT button.

Is there any way to intercept the PTT button when the application is in the background?

Thank you

Shrievalty answered 25/8, 2016 at 13:42 Comment(2)
Android does not have a PTT button. You will need to ask your device manufacturer how to implement this. From an onKeyDown() standpoint, they seem to be using KEYCODE_CAMERA, so maybe ACTION_CAMERA_BUTTON will work.Rowney
thanks, I imagined and tried in every way without success. I'll try to contact the device manufacturer.Shrievalty
B
9

The solution to your question depends highly on the device you are using. If you have a phone with a dedicated PTT button, the manufacturer of the phone almost certainly has made an Intent available for app developers such as yourself to intercept PTT down and up events, but you'll need to contact the manufacturer for more information.

For instance, phones from Kyocera, Sonim, and Casio have such Intents available, and you'd simply need to put a receiver declaration in your AndroidManifest.xml, like this for a Kyocera phone:

    <receiver android:exported="true" android:name="com.myapp.receiver.KeyReceiverKyocera">
        <intent-filter android:priority="9999999">
            <action android:name="com.kodiak.intent.action.PTT_BUTTON" />
            <action android:name="com.kyocera.android.intent.action.PTT_BUTTON" />
        </intent-filter>
    </receiver>

Then, a simple BroadcastReceiver class that receives the up and down intents:

public class KeyReceiverKyocera extends BroadcastReceiver
{
    private static boolean keyDown = false;

    @Override
    public void onReceive(Context context, Intent intent)
    {
        String action = intent.getAction();

        if (keyDown)
            processPTTDown();
        else
            processPTTUp();

        keyDown = !keyDown;
    }
}

Hope this helps,

Shawn

Bornie answered 8/11, 2016 at 18:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.