List of usage examples for android.view KeyEvent KEYCODE_HEADSETHOOK
int KEYCODE_HEADSETHOOK
To view the source code for android.view KeyEvent KEYCODE_HEADSETHOOK.
Click Source Link
From source file:com.koma.music.service.MediaButtonIntentReceiver.java
@Override public void onReceive(final Context context, final Intent intent) { LogUtils.i(TAG, "Received intent: " + intent); final String intentAction = intent.getAction(); if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intentAction)) { startService(context, MusicServiceConstants.CMDPAUSE, System.currentTimeMillis()); } else if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) { final KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); if (event == null || event.getAction() != KeyEvent.ACTION_UP) { return; }/*w ww. ja v a 2 s . c o m*/ String command = null; switch (event.getKeyCode()) { case KeyEvent.KEYCODE_HEADSETHOOK: command = MusicServiceConstants.CMDHEADSETHOOK; break; case KeyEvent.KEYCODE_MEDIA_STOP: command = MusicServiceConstants.CMDSTOP; break; case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: command = MusicServiceConstants.CMDTOGGLEPAUSE; break; case KeyEvent.KEYCODE_MEDIA_NEXT: command = MusicServiceConstants.CMDNEXT; break; case KeyEvent.KEYCODE_MEDIA_PREVIOUS: command = MusicServiceConstants.CMDPREVIOUS; break; case KeyEvent.KEYCODE_MEDIA_PAUSE: command = MusicServiceConstants.CMDPAUSE; break; case KeyEvent.KEYCODE_MEDIA_PLAY: command = MusicServiceConstants.CMDPLAY; break; } if (command != null) { startService(context, command, event.getEventTime()); if (isOrderedBroadcast()) { abortBroadcast(); } } } }
From source file:org.videolan.vlc.RemoteControlClientReceiver.java
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); if (event != null && action.equalsIgnoreCase(Intent.ACTION_MEDIA_BUTTON)) { if (event.getKeyCode() != KeyEvent.KEYCODE_HEADSETHOOK && event.getKeyCode() != KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE && event.getAction() != KeyEvent.ACTION_DOWN) { super.onReceive(context, intent); return; }/*from w w w.j ava 2 s .c om*/ Intent i = null; switch (event.getKeyCode()) { /* * one click => play/pause * long click => previous * double click => next */ case KeyEvent.KEYCODE_HEADSETHOOK: case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: long time = SystemClock.uptimeMillis(); switch (event.getAction()) { case KeyEvent.ACTION_DOWN: if (event.getRepeatCount() <= 0) mHeadsetDownTime = time; break; case KeyEvent.ACTION_UP: if (AndroidDevices.hasTsp()) { //no backward/forward on TV if (time - mHeadsetDownTime >= 1000) { // long click i = new Intent(PlaybackService.ACTION_REMOTE_BACKWARD, null, VLCApplication.getAppContext(), PlaybackService.class); break; } else if (time - mHeadsetUpTime <= 500) { // double click i = new Intent(PlaybackService.ACTION_REMOTE_FORWARD, null, VLCApplication.getAppContext(), PlaybackService.class); break; } } // one click i = new Intent(PlaybackService.ACTION_REMOTE_PLAYPAUSE, null, VLCApplication.getAppContext(), PlaybackService.class); mHeadsetUpTime = time; break; } break; case KeyEvent.KEYCODE_MEDIA_PLAY: context.startService(new Intent(PlaybackService.ACTION_REMOTE_PLAY, null, VLCApplication.getAppContext(), PlaybackService.class)); return; case KeyEvent.KEYCODE_MEDIA_PAUSE: i = new Intent(PlaybackService.ACTION_REMOTE_PAUSE, null, VLCApplication.getAppContext(), PlaybackService.class); break; case KeyEvent.KEYCODE_MEDIA_STOP: i = new Intent(PlaybackService.ACTION_REMOTE_STOP, null, VLCApplication.getAppContext(), PlaybackService.class); break; case KeyEvent.KEYCODE_MEDIA_NEXT: i = new Intent(PlaybackService.ACTION_REMOTE_FORWARD, null, VLCApplication.getAppContext(), PlaybackService.class); break; case KeyEvent.KEYCODE_MEDIA_PREVIOUS: i = new Intent(PlaybackService.ACTION_REMOTE_BACKWARD, null, VLCApplication.getAppContext(), PlaybackService.class); break; } if (isOrderedBroadcast()) abortBroadcast(); if (i != null) { context.startService(i); return; } } else if (action.equals(PlaybackService.ACTION_REMOTE_PLAYPAUSE)) { intent = new Intent(context, PlaybackService.class); intent.setAction(PlaybackService.ACTION_REMOTE_PLAYPAUSE); context.startService(intent); return; } super.onReceive(context, intent); }
From source file:org.interactiverobotics.headset_launcher.MediaButtonMonitorService.java
@Override public void onCreate() { Log.d(TAG, "Create"); // ? /*from w ww. j a v a 2s . c o m*/ mMediaButtonReceiver = new ComponentName(this, MediaButtonReceiver.class); // , ? API 18+ // // final Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); // // mediaButtonIntent.setComponent(mMediaButtonReceiver); // // mPendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0); mMediaSession = new MediaSessionCompat(this, TAG, mMediaButtonReceiver, /* pendingIntent */ null); mMediaSession.setCallback(new MediaSessionCompat.Callback() { @Override public boolean onMediaButtonEvent(Intent mediaButtonEvent) { if (Intent.ACTION_MEDIA_BUTTON.equals(mediaButtonEvent.getAction())) { final KeyEvent keyEvent = (KeyEvent) mediaButtonEvent .getParcelableExtra(Intent.EXTRA_KEY_EVENT); if (KeyEvent.KEYCODE_HEADSETHOOK == keyEvent.getKeyCode()) { if (KeyEvent.ACTION_DOWN == keyEvent.getAction()) { Log.d(TAG, "Media button down!"); // ? startService(new Intent(MediaButtonMonitorService.this, LauncherService.class)); } } else { Log.e(TAG, "Unknown keycode: " + keyEvent.getKeyCode()); } } else { Log.e(TAG, "Unknown intent: " + mediaButtonEvent.getAction()); } return true; } }); mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS); mMediaSession.setActive(true); }
From source file:com.dragedy.playermusic.service.MediaButtonIntentReceiver.java
public static boolean handleIntent(final Context context, final Intent intent) { final String intentAction = intent.getAction(); if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) { final KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); if (event == null) { return false; }// www . java 2s . c o m final int keycode = event.getKeyCode(); final int action = event.getAction(); final long eventTime = event.getEventTime(); String command = null; switch (keycode) { case KeyEvent.KEYCODE_MEDIA_STOP: command = MusicService.ACTION_STOP; break; case KeyEvent.KEYCODE_HEADSETHOOK: case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: command = MusicService.ACTION_TOGGLE_PAUSE; break; case KeyEvent.KEYCODE_MEDIA_NEXT: command = MusicService.ACTION_SKIP; break; case KeyEvent.KEYCODE_MEDIA_PREVIOUS: command = MusicService.ACTION_REWIND; break; case KeyEvent.KEYCODE_MEDIA_PAUSE: command = MusicService.ACTION_PAUSE; break; case KeyEvent.KEYCODE_MEDIA_PLAY: command = MusicService.ACTION_PLAY; break; } if (command != null) { if (action == KeyEvent.ACTION_DOWN) { if (event.getRepeatCount() == 0) { // Only consider the first event in a sequence, not the repeat events, // so that we don't trigger in cases where the first event went to // a different app (e.g. when the user ends a phone call by // long pressing the headset button) // The service may or may not be running, but we need to send it // a command. if (keycode == KeyEvent.KEYCODE_HEADSETHOOK) { if (eventTime - mLastClickTime >= DOUBLE_CLICK) { mClickCounter = 0; } mClickCounter++; if (DEBUG) Log.v(TAG, "Got headset click, count = " + mClickCounter); mHandler.removeMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT); Message msg = mHandler.obtainMessage(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT, mClickCounter, 0, context); long delay = mClickCounter < 3 ? DOUBLE_CLICK : 0; if (mClickCounter >= 3) { mClickCounter = 0; } mLastClickTime = eventTime; acquireWakeLockAndSendMessage(context, msg, delay); } else { startService(context, command); } return true; } } } } return false; }
From source file:Main.java
public static String getKeyNameByCode(int code) { switch (code) { case KeyEvent.KEYCODE_0: return "<0>"; case KeyEvent.KEYCODE_1: return "<1>"; case KeyEvent.KEYCODE_2: return "<2>"; case KeyEvent.KEYCODE_3: return "<3>"; case KeyEvent.KEYCODE_4: return "<4>"; case KeyEvent.KEYCODE_5: return "<5>"; case KeyEvent.KEYCODE_6: return "<6>"; case KeyEvent.KEYCODE_7: return "<7>"; case KeyEvent.KEYCODE_8: return "<8>"; case KeyEvent.KEYCODE_9: return "<9>"; case KeyEvent.KEYCODE_A: return "<A>"; case KeyEvent.KEYCODE_B: return "<B>"; case KeyEvent.KEYCODE_C: return "<C>"; case KeyEvent.KEYCODE_D: return "<D>"; case KeyEvent.KEYCODE_E: return "<E>"; case KeyEvent.KEYCODE_F: return "<F>"; case KeyEvent.KEYCODE_G: return "<G>"; case KeyEvent.KEYCODE_H: return "<H>"; case KeyEvent.KEYCODE_I: return "<I>"; case KeyEvent.KEYCODE_J: return "<J>"; case KeyEvent.KEYCODE_K: return "<K>"; case KeyEvent.KEYCODE_L: return "<L>"; case KeyEvent.KEYCODE_M: return "<M>"; case KeyEvent.KEYCODE_N: return "<N>"; case KeyEvent.KEYCODE_O: return "<O>"; case KeyEvent.KEYCODE_P: return "<P>"; case KeyEvent.KEYCODE_Q: return "<Q>"; case KeyEvent.KEYCODE_R: return "<R>"; case KeyEvent.KEYCODE_S: return "<S>"; case KeyEvent.KEYCODE_T: return "<T>"; case KeyEvent.KEYCODE_U: return "<U>"; case KeyEvent.KEYCODE_V: return "<V>"; case KeyEvent.KEYCODE_W: return "<W>"; case KeyEvent.KEYCODE_X: return "<X>"; case KeyEvent.KEYCODE_Y: return "<Y>"; case KeyEvent.KEYCODE_Z: return "<Z>"; case KeyEvent.KEYCODE_APOSTROPHE: return "<'>"; case KeyEvent.KEYCODE_AT: return "<@>"; case KeyEvent.KEYCODE_BACK: return "<Back>"; case KeyEvent.KEYCODE_BACKSLASH: return "<\\>"; case KeyEvent.KEYCODE_CALL: return "<Call>"; case KeyEvent.KEYCODE_CAMERA: return "<Camera>"; case KeyEvent.KEYCODE_CLEAR: return "<Clear>"; case KeyEvent.KEYCODE_COMMA: return "<,>"; case KeyEvent.KEYCODE_DEL: return "<Del>"; case KeyEvent.KEYCODE_DPAD_CENTER: return "<PadCenter>"; case KeyEvent.KEYCODE_DPAD_DOWN: return "<PadDown>"; case KeyEvent.KEYCODE_DPAD_LEFT: return "<PadLeft>"; case KeyEvent.KEYCODE_DPAD_RIGHT: return "<PadRight>"; case KeyEvent.KEYCODE_DPAD_UP: return "<PadUp>"; case KeyEvent.KEYCODE_ENDCALL: return "<EndCall>"; case KeyEvent.KEYCODE_ENTER: return "<Enter>"; case KeyEvent.KEYCODE_ENVELOPE: return "<Envelope>"; case KeyEvent.KEYCODE_EQUALS: return "<=>"; case KeyEvent.KEYCODE_EXPLORER: return "<Explorer>"; case KeyEvent.KEYCODE_FOCUS: return "<??? 0>"; case KeyEvent.KEYCODE_GRAVE: return "<??? 1>"; case KeyEvent.KEYCODE_HEADSETHOOK: return "<??? 2>"; case KeyEvent.KEYCODE_HOME: return "<Home>"; case KeyEvent.KEYCODE_LEFT_BRACKET: return "<(>"; case KeyEvent.KEYCODE_MENU: return "<Menu>"; case KeyEvent.KEYCODE_MINUS: return "<->"; case KeyEvent.KEYCODE_NOTIFICATION: return "<??? 3>"; case KeyEvent.KEYCODE_NUM: return "<Num>"; case KeyEvent.KEYCODE_PERIOD: return "<??? 4>"; case KeyEvent.KEYCODE_PLUS: return "<+>"; case KeyEvent.KEYCODE_POUND: return "<??? 5>"; case KeyEvent.KEYCODE_POWER: return "<Power>"; case KeyEvent.KEYCODE_RIGHT_BRACKET: return "<)>"; case KeyEvent.KEYCODE_SEMICOLON: return "<;>"; case KeyEvent.KEYCODE_SLASH: return "</>"; case KeyEvent.KEYCODE_SOFT_LEFT: return "<??? 6>"; case KeyEvent.KEYCODE_SOFT_RIGHT: return "<??? 7>"; case KeyEvent.KEYCODE_SPACE: return "<Space>"; case KeyEvent.KEYCODE_STAR: return "<*>"; case KeyEvent.KEYCODE_SYM: return "<Sym>"; case KeyEvent.KEYCODE_TAB: return "<Tab>"; case KeyEvent.KEYCODE_VOLUME_DOWN: return "<VolumeDown>"; case KeyEvent.KEYCODE_VOLUME_UP: return "<VolumeUp>"; case KeyEvent.KEYCODE_UNKNOWN: default:// ww w . j a v a 2 s . c om return "<Unknown>"; } }
From source file:com.rks.musicx.services.MediaButtonReceiver.java
@Override public void onReceive(Context context, Intent intent) { String intentAction = intent.getAction(); String command = null;/* www.ja va 2 s . co m*/ if (intent.getAction() != null) { if (intentAction.equals(Intent.ACTION_MEDIA_BUTTON)) { KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); if (event == null) { return; } int keycode = event.getKeyCode(); final int action = event.getAction(); final long eventTime = event.getEventTime(); switch (keycode) { case KeyEvent.KEYCODE_MEDIA_STOP: Log.d(TAG, "stop"); command = Constants.ACTION_STOP; break; case KeyEvent.KEYCODE_HEADSETHOOK: case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: Log.d(TAG, "toggle"); command = Constants.ACTION_TOGGLE; break; case KeyEvent.KEYCODE_MEDIA_NEXT: Log.d(TAG, "next"); command = Constants.ACTION_NEXT; break; case KeyEvent.KEYCODE_MEDIA_PREVIOUS: Log.d(TAG, "prev"); command = Constants.ACTION_PREVIOUS; break; case KeyEvent.KEYCODE_MEDIA_PAUSE: Log.d(TAG, "pause"); command = Constants.ACTION_PAUSE; break; case KeyEvent.KEYCODE_MEDIA_PLAY: Log.d(TAG, "play"); command = Constants.ACTION_PLAY; break; } // startServices(context, command); if (command != null) { if (action == KeyEvent.ACTION_DOWN) { if (event.getRepeatCount() == 0) { if (keycode == KeyEvent.KEYCODE_HEADSETHOOK) { if (eventTime - mLastClickTime >= DOUBLE_CLICK) { mClickCounter = 0; } mClickCounter++; Log.e("MediaButton", "Got headset click, count = " + mClickCounter); mHandler.removeMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT); Message msg = mHandler.obtainMessage(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT, mClickCounter, 0, context); long delay = mClickCounter < 3 ? DOUBLE_CLICK : 0; if (mClickCounter >= 3) { mClickCounter = 0; } mLastClickTime = eventTime; acquireWakeLockAndSendMessage(context, msg, delay); } else { startServices(context, command); } } } } } } }
From source file:com.gecq.musicwave.player.MediaButtonIntentReceiver.java
/** * {@inheritDoc}//w ww .java 2 s . co m */ @Override public void onReceive(final Context context, final Intent intent) { if (DEBUG) Log.v(TAG, "Received intent: " + intent); final String intentAction = intent.getAction(); if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intentAction)) { startService(context, PlayerService.CMDPAUSE); } else if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) { final KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); if (event == null) { return; } final int keycode = event.getKeyCode(); final int action = event.getAction(); final long eventtime = event.getEventTime(); String command = null; switch (keycode) { case KeyEvent.KEYCODE_MEDIA_STOP: command = PlayerService.CMDSTOP; break; case KeyEvent.KEYCODE_HEADSETHOOK: case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: command = PlayerService.CMDTOGGLEPAUSE; break; case KeyEvent.KEYCODE_MEDIA_NEXT: command = PlayerService.CMDNEXT; break; case KeyEvent.KEYCODE_MEDIA_PREVIOUS: command = PlayerService.CMDPREVIOUS; break; case KeyEvent.KEYCODE_MEDIA_PAUSE: command = PlayerService.CMDPAUSE; break; case KeyEvent.KEYCODE_MEDIA_PLAY: command = PlayerService.CMDPLAY; break; } if (command != null) { if (action == KeyEvent.ACTION_DOWN) { if (mDown) { if (PlayerService.CMDTOGGLEPAUSE.equals(command) || PlayerService.CMDPLAY.equals(command)) { if (mLastClickTime != 0 && eventtime - mLastClickTime > LONG_PRESS_DELAY) { acquireWakeLockAndSendMessage(context, mHandler.obtainMessage(MSG_LONGPRESS_TIMEOUT, context), 0); } } } else if (event.getRepeatCount() == 0) { // Only consider the first event in a sequence, not the repeat events, // so that we don't trigger in cases where the first event went to // a different app (e.g. when the user ends a phone call by // long pressing the headset button) // The service may or may not be running, but we need to send it // a command. if (keycode == KeyEvent.KEYCODE_HEADSETHOOK) { if (eventtime - mLastClickTime >= DOUBLE_CLICK) { mClickCounter = 0; } mClickCounter++; if (DEBUG) Log.v(TAG, "Got headset click, count = " + mClickCounter); mHandler.removeMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT); Message msg = mHandler.obtainMessage(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT, mClickCounter, 0, context); long delay = mClickCounter < 3 ? DOUBLE_CLICK : 0; if (mClickCounter >= 3) { mClickCounter = 0; } mLastClickTime = eventtime; acquireWakeLockAndSendMessage(context, msg, delay); } else { startService(context, command); } mLaunched = false; mDown = true; } } else { mHandler.removeMessages(MSG_LONGPRESS_TIMEOUT); mDown = false; } if (isOrderedBroadcast()) { abortBroadcast(); } releaseWakeLockIfHandlerIdle(); } } }
From source file:com.andrew.apollo.MediaButtonIntentReceiver.java
/** * {@inheritDoc}/*from ww w .j a va2s.c o m*/ */ @Override public void onReceive(final Context context, final Intent intent) { if (DEBUG) Log.v(TAG, "Received intent: " + intent); final String intentAction = intent.getAction(); if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intentAction)) { startService(context, MusicPlaybackService.CMDPAUSE); } else if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) { final KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); if (event == null) { return; } final int keycode = event.getKeyCode(); final int action = event.getAction(); final long eventtime = event.getEventTime(); String command = null; switch (keycode) { case KeyEvent.KEYCODE_MEDIA_STOP: command = MusicPlaybackService.CMDSTOP; break; case KeyEvent.KEYCODE_HEADSETHOOK: case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: command = MusicPlaybackService.CMDTOGGLEPAUSE; break; case KeyEvent.KEYCODE_MEDIA_NEXT: command = MusicPlaybackService.CMDNEXT; break; case KeyEvent.KEYCODE_MEDIA_PREVIOUS: command = MusicPlaybackService.CMDPREVIOUS; break; case KeyEvent.KEYCODE_MEDIA_PAUSE: command = MusicPlaybackService.CMDPAUSE; break; case KeyEvent.KEYCODE_MEDIA_PLAY: command = MusicPlaybackService.CMDPLAY; break; } if (command != null) { if (action == KeyEvent.ACTION_DOWN) { if (mDown) { if (MusicPlaybackService.CMDTOGGLEPAUSE.equals(command) || MusicPlaybackService.CMDPLAY.equals(command)) { if (mLastClickTime != 0 && eventtime - mLastClickTime > LONG_PRESS_DELAY) { acquireWakeLockAndSendMessage(context, mHandler.obtainMessage(MSG_LONGPRESS_TIMEOUT, context), 0); } } } else if (event.getRepeatCount() == 0) { // Only consider the first event in a sequence, not the repeat events, // so that we don't trigger in cases where the first event went to // a different app (e.g. when the user ends a phone call by // long pressing the headset button) // The service may or may not be running, but we need to send it // a command. if (keycode == KeyEvent.KEYCODE_HEADSETHOOK) { if (eventtime - mLastClickTime >= DOUBLE_CLICK) { mClickCounter = 0; } mClickCounter++; if (DEBUG) Log.v(TAG, "Got headset click, count = " + mClickCounter); mHandler.removeMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT); Message msg = mHandler.obtainMessage(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT, mClickCounter, 0, context); long delay = mClickCounter < 3 ? DOUBLE_CLICK : 0; if (mClickCounter >= 3) { mClickCounter = 0; } mLastClickTime = eventtime; acquireWakeLockAndSendMessage(context, msg, delay); } else { startService(context, command); } mLaunched = false; mDown = true; } } else { mHandler.removeMessages(MSG_LONGPRESS_TIMEOUT); mDown = false; } if (isOrderedBroadcast()) { abortBroadcast(); } releaseWakeLockIfHandlerIdle(); } } }
From source file:singh.amandeep.musicplayer.MediaButtonIntentReceiver.java
/** * {@inheritDoc}//from w w w .java 2s . com */ @Override public void onReceive(final Context context, final Intent intent) { final String intentAction = intent.getAction(); if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intentAction)) { startService(context, MusicService.CMDPAUSE); } else if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) { final KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); if (event == null) { return; } final int keycode = event.getKeyCode(); final int action = event.getAction(); final long eventtime = event.getEventTime(); String command = null; switch (keycode) { case KeyEvent.KEYCODE_MEDIA_STOP: command = MusicService.CMDSTOP; break; case KeyEvent.KEYCODE_HEADSETHOOK: case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: command = MusicService.CMDTOGGLEPAUSE; break; case KeyEvent.KEYCODE_MEDIA_NEXT: command = MusicService.CMDNEXT; break; case KeyEvent.KEYCODE_MEDIA_PREVIOUS: command = MusicService.CMDPREVIOUS; break; case KeyEvent.KEYCODE_MEDIA_PAUSE: command = MusicService.CMDPAUSE; break; case KeyEvent.KEYCODE_MEDIA_PLAY: command = MusicService.CMDPLAY; break; } if (command != null) { if (action == KeyEvent.ACTION_DOWN) { if (mDown) { if (MusicService.CMDTOGGLEPAUSE.equals(command) || MusicService.CMDPLAY.equals(command)) { if (mLastClickTime != 0 && eventtime - mLastClickTime > LONG_PRESS_DELAY) { acquireWakeLockAndSendMessage(context, mHandler.obtainMessage(MSG_LONGPRESS_TIMEOUT, context), 0); } } } else if (event.getRepeatCount() == 0) { // Only consider the first event in a sequence, not the repeat events, // so that we don't trigger in cases where the first event went to // a different app (e.g. when the user ends a phone call by // long pressing the headset button) // The service may or may not be running, but we need to send it // a command. if (keycode == KeyEvent.KEYCODE_HEADSETHOOK) { if (eventtime - mLastClickTime >= DOUBLE_CLICK) { mClickCounter = 0; } mClickCounter++; mHandler.removeMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT); Message msg = mHandler.obtainMessage(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT, mClickCounter, 0, context); long delay = mClickCounter < 3 ? DOUBLE_CLICK : 0; if (mClickCounter >= 3) { mClickCounter = 0; } mLastClickTime = eventtime; acquireWakeLockAndSendMessage(context, msg, delay); } else { startService(context, command); } mLaunched = false; mDown = true; } } else { mHandler.removeMessages(MSG_LONGPRESS_TIMEOUT); mDown = false; } if (isOrderedBroadcast()) { abortBroadcast(); } releaseWakeLockIfHandlerIdle(); } } }
From source file:com.av.remusic.receiver.MediaButtonIntentReceiver.java
@Override public void onReceive(final Context context, final Intent intent) { final String intentAction = intent.getAction(); if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intentAction)) { if (true) startService(context, MediaService.CMDPAUSE); } else if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) { final KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); if (event == null) { return; }/*w ww .jav a 2 s. co m*/ final int keycode = event.getKeyCode(); final int action = event.getAction(); final long eventtime = event.getEventTime(); String command = null; switch (keycode) { case KeyEvent.KEYCODE_MEDIA_STOP: command = MediaService.CMDSTOP; break; case KeyEvent.KEYCODE_HEADSETHOOK: case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: command = MediaService.CMDTOGGLEPAUSE; break; case KeyEvent.KEYCODE_MEDIA_NEXT: command = MediaService.CMDNEXT; break; case KeyEvent.KEYCODE_MEDIA_PREVIOUS: command = MediaService.CMDPREVIOUS; break; case KeyEvent.KEYCODE_MEDIA_PAUSE: command = MediaService.CMDPAUSE; break; case KeyEvent.KEYCODE_MEDIA_PLAY: command = MediaService.CMDPLAY; break; } if (command != null) { if (action == KeyEvent.ACTION_DOWN) { if (mDown) { if (MediaService.CMDTOGGLEPAUSE.equals(command) || MediaService.CMDPLAY.equals(command)) { if (mLastClickTime != 0 && eventtime - mLastClickTime > LONG_PRESS_DELAY) { acquireWakeLockAndSendMessage(context, mHandler.obtainMessage(MSG_LONGPRESS_TIMEOUT, context), 0); } } } else if (event.getRepeatCount() == 0) { if (keycode == KeyEvent.KEYCODE_HEADSETHOOK) { if (eventtime - mLastClickTime >= DOUBLE_CLICK) { mClickCounter = 0; } mClickCounter++; if (DEBUG) Log.v(TAG, "Got headset click, count = " + mClickCounter); mHandler.removeMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT); Message msg = mHandler.obtainMessage(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT, mClickCounter, 0, context); long delay = mClickCounter < 3 ? DOUBLE_CLICK : 0; if (mClickCounter >= 3) { mClickCounter = 0; } mLastClickTime = eventtime; acquireWakeLockAndSendMessage(context, msg, delay); } else { startService(context, command); } mLaunched = false; mDown = true; } } else { mHandler.removeMessages(MSG_LONGPRESS_TIMEOUT); mDown = false; } if (isOrderedBroadcast()) { abortBroadcast(); } releaseWakeLockIfHandlerIdle(); } } }