Reputation: 1
Welcome to my first question in this forum.
I am building an alarm informing SMS about burglary.
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
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