Jaco Hakim
Jaco Hakim

Reputation: 1

Detect pressing or releasing (no pressing and releasing) the headset key on Android

Welcome to my first question in this forum.

I am building an alarm informing SMS about burglary.

  1. I put JACK in the phone.
  2. I connect the GND and MIC contacts at the door with a long wire.
  3. When someone closes or opens the circuit, the phone sends an SMS.

With the BroadcastReceiver (source code below), I can easily detect pressing and releasing the PLAY / PAUSE key on a wired headset. But BroadcastReceiver only starts after short pressing and releasing the PLAY / PAUSE button.

I need to immediately detect when the headset key was only pressed or just only released. The time between pressing and releasing can be very long, for example, a few days. The solution detecting the power interruption of the phone or the sensor on Bluetooth (AT commands), unfortunately, fall off. Android is 4.0.4.

    public class MediaButtonReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        String intentAction = intent.getAction();
        Log.i ("TAG_MEDIA", intentAction.toString() + " happended");
        if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
            Log.i ("TAG_MEDIA", "no media button information");
            return;
        }
        KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null) {
            Log.i ("TAG_MEDIA", "no keypress");
            return;
        }
        Log.i ("TAG_MEDIA", "Yes keypress = " + event);
        }

    }

Upvotes: 0

Views: 108

Answers (1)

Minja Denis
Minja Denis

Reputation: 1

    if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK) {

        if (status.equals("recording")) {
            txt_recStatus.setText("save recording..");
            stopRecording();
        } else {
            Log.e("headset565656", "record now:");
            //start record
            showTimer();
            recordAudio();

        }
        return true;
    }

Upvotes: 0

Related Questions