List of usage examples for android.content Intent ACTION_MEDIA_BUTTON
String ACTION_MEDIA_BUTTON
To view the source code for android.content Intent ACTION_MEDIA_BUTTON.
Click Source Link
From source file:com.gecq.musicwave.player.MediaButtonIntentReceiver.java
/** * {@inheritDoc}/*from w w w . ja va 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 w w w. java 2 s . com */ @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:androidx.media.session.MediaButtonReceiver.java
@Override public void onReceive(Context context, Intent intent) { if (intent == null || !Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction()) || !intent.hasExtra(Intent.EXTRA_KEY_EVENT)) { Log.d(TAG, "Ignore unsupported intent: " + intent); return;// w ww. ja v a 2 s .c om } ComponentName mediaButtonServiceComponentName = getServiceComponentByAction(context, Intent.ACTION_MEDIA_BUTTON); if (mediaButtonServiceComponentName != null) { intent.setComponent(mediaButtonServiceComponentName); startForegroundService(context, intent); return; } ComponentName mediaBrowserServiceComponentName = getServiceComponentByAction(context, MediaBrowserServiceCompat.SERVICE_INTERFACE); if (mediaBrowserServiceComponentName != null) { PendingResult pendingResult = goAsync(); Context applicationContext = context.getApplicationContext(); MediaButtonConnectionCallback connectionCallback = new MediaButtonConnectionCallback(applicationContext, intent, pendingResult); MediaBrowserCompat mediaBrowser = new MediaBrowserCompat(applicationContext, mediaBrowserServiceComponentName, connectionCallback, null); connectionCallback.setMediaBrowser(mediaBrowser); mediaBrowser.connect(); return; } throw new IllegalStateException("Could not find any Service that handles " + Intent.ACTION_MEDIA_BUTTON + " or implements a media browser service."); }
From source file:org.amahi.anywhere.service.AudioService.java
private void setUpAudioPlayerRemote() { AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); ComponentName audioReceiver = new ComponentName(getPackageName(), AudioReceiver.class.getName()); Intent audioIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); audioIntent.setComponent(audioReceiver); PendingIntent audioPendingIntent = PendingIntent.getBroadcast(this, 0, audioIntent, 0); audioPlayerRemote = new RemoteControlClient(audioPendingIntent); audioPlayerRemote.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS); audioPlayerRemote.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING); audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); audioManager.registerMediaButtonEventReceiver(audioReceiver); audioManager.registerRemoteControlClient(audioPlayerRemote); }
From source file:fm.libre.droid.LibreDroid.java
/** Called when the activity is first created. */ @Override/*from w ww. j ava 2s . c o m*/ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); libreServiceConn = new LibreServiceConnection(); bindService(new Intent(this, LibreService.class), libreServiceConn, Context.BIND_AUTO_CREATE); this.registerReceiver(new MediaButtonReceiver(), new IntentFilter(Intent.ACTION_MEDIA_BUTTON)); this.registerReceiver(new UIUpdateReceiver(), new IntentFilter("LibreDroidNewSong")); setContentView(R.layout.main); // Load settings final SharedPreferences settings = getSharedPreferences("LibreDroid", MODE_PRIVATE); username = settings.getString("Username", ""); password = settings.getString("Password", ""); final EditText usernameEntry = (EditText) findViewById(R.id.usernameEntry); final EditText passwordEntry = (EditText) findViewById(R.id.passwordEntry); usernameEntry.setText(username); passwordEntry.setText(password); final Button loginButton = (Button) findViewById(R.id.loginButton); loginButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { Editor editor = settings.edit(); editor.putString("Username", usernameEntry.getText().toString()); editor.putString("Password", passwordEntry.getText().toString()); editor.commit(); LibreDroid.this.login(); } }); stations = new ArrayList<String>(); try { BufferedReader stationReader = new BufferedReader( new InputStreamReader(openFileInput("libredroid-custom-stations.conf"))); String station; while ((station = stationReader.readLine()) != null) { stations.add(station); } stationReader.close(); } catch (IOException ex) { Log.d("libredroid", ex.getMessage()); } // Add default stations if empty if (stations.isEmpty()) { String radioButtons[] = { "Folk", "Rock", "Metal", "Classical", "Pop", "Punk", "Jazz", "Blues", "Rap", "Ambient", "Add A Custom Station..." }; stations.addAll(Arrays.asList(radioButtons)); } setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, stations)); Button tagStation = (Button) findViewById(R.id.tagStationButton); tagStation.setOnClickListener(new OnClickListener() { public void onClick(View v) { LibreDroid.this.nextPage(); } }); Button loveStation = (Button) findViewById(R.id.loveStationButton); loveStation.setOnClickListener(new OnClickListener() { public void onClick(View v) { LibreDroid.this.libreServiceConn.service.tuneStation("user", username + "/loved"); LibreDroid.this.nextPage(); LibreDroid.this.nextPage(); LibreDroid.this.nextPage(); } }); Button communityLoveStation = (Button) findViewById(R.id.communityStationButton); communityLoveStation.setOnClickListener(new OnClickListener() { public void onClick(View v) { LibreDroid.this.libreServiceConn.service.tuneStation("community", "loved"); LibreDroid.this.nextPage(); LibreDroid.this.nextPage(); LibreDroid.this.nextPage(); } }); final ImageButton nextButton = (ImageButton) findViewById(R.id.nextButton); nextButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { LibreDroid.this.libreServiceConn.service.next(); } }); final ImageButton prevButton = (ImageButton) findViewById(R.id.prevButton); prevButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { LibreDroid.this.libreServiceConn.service.prev(); } }); final ImageButton playPauseButton = (ImageButton) findViewById(R.id.playPauseButton); playPauseButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { LibreDroid.this.togglePause(); } }); final ImageButton saveButton = (ImageButton) findViewById(R.id.saveButton); saveButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { LibreDroid.this.save(); } }); final ImageButton loveButton = (ImageButton) findViewById(R.id.loveButton); loveButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { LibreDroid.this.libreServiceConn.service.love(); } }); final ImageButton banButton = (ImageButton) findViewById(R.id.banButton); banButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { LibreDroid.this.libreServiceConn.service.ban(); } }); final Button addStationButton = (Button) findViewById(R.id.addStationButton); addStationButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { LibreDroid.this.addStation(); } }); }
From source file:com.dmplayer.manager.MusicPlayerService.java
@SuppressLint("NewApi") @Override//from w ww . ja va 2 s . co m public int onStartCommand(Intent intent, int flags, int startId) { try { SongDetail messageObject = MediaController.getInstance().getPlayingSongDetail(); if (messageObject == null) { DMPlayerUtility.runOnUIThread(new Runnable() { @Override public void run() { stopSelf(); } }); return START_STICKY; } if (supportLockScreenControls) { ComponentName remoteComponentName = new ComponentName(getApplicationContext(), MusicPlayerReceiver.class.getName()); try { if (remoteControlClient == null) { audioManager.registerMediaButtonEventReceiver(remoteComponentName); Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.setComponent(remoteComponentName); PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0); remoteControlClient = new RemoteControlClient(mediaPendingIntent); audioManager.registerRemoteControlClient(remoteControlClient); } remoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_STOP | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_NEXT); } catch (Exception e) { Log.e("tmessages", e.toString()); } } createNotification(messageObject); } catch (Exception e) { e.printStackTrace(); } return START_STICKY; }
From source file:github.popeen.dsub.util.compat.RemoteControlClientLP.java
@Override public void register(Context context, ComponentName mediaButtonReceiverComponent) { downloadService = (DownloadService) context; mediaSession = new MediaSessionCompat(downloadService, "DSub MediaSession"); Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.setComponent(mediaButtonReceiverComponent); PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, mediaButtonIntent, 0);/*from w ww . j a va 2 s. c o m*/ mediaSession.setMediaButtonReceiver(mediaPendingIntent); Intent activityIntent = new Intent(context, SubsonicFragmentActivity.class); activityIntent.putExtra(Constants.INTENT_EXTRA_NAME_DOWNLOAD, true); activityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent activityPendingIntent = PendingIntent.getActivity(context, 0, activityIntent, 0); mediaSession.setSessionActivity(activityPendingIntent); mediaSession.setFlags( MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS | MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS); mediaSession.setCallback(new EventCallback()); mediaSession.setPlaybackToLocal(AudioManager.STREAM_MUSIC); mediaSession.setActive(true); Bundle sessionExtras = new Bundle(); sessionExtras.putBoolean(WEAR_BACKGROUND_THEME, true); sessionExtras.putBoolean(WEAR_RESERVE_SKIP_TO_PREVIOUS, true); sessionExtras.putBoolean(WEAR_RESERVE_SKIP_TO_NEXT, true); sessionExtras.putBoolean(AUTO_RESERVE_SKIP_TO_PREVIOUS, true); sessionExtras.putBoolean(AUTO_RESERVE_SKIP_TO_NEXT, true); mediaSession.setExtras(sessionExtras); imageLoader = SubsonicActivity.getStaticImageLoader(context); }
From source file:in.risysnetworks.shplayer.mediacontroller.MusicPlayerService.java
@SuppressLint("NewApi") @Override//from w w w . jav a 2 s . c o m public int onStartCommand(Intent intent, int flags, int startId) { try { SongDetail messageObject = MediaController.getInstance().getPlayingSongDetail(); if (messageObject == null) { SHPlayerUtility.runOnUIThread(new Runnable() { @Override public void run() { stopSelf(); } }); return START_STICKY; } if (supportLockScreenControls) { ComponentName remoteComponentName = new ComponentName(getApplicationContext(), MusicPlayerReceiver.class.getName()); try { if (remoteControlClient == null) { audioManager.registerMediaButtonEventReceiver(remoteComponentName); Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.setComponent(remoteComponentName); PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0); remoteControlClient = new RemoteControlClient(mediaPendingIntent); audioManager.registerRemoteControlClient(remoteControlClient); } remoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_STOP | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_NEXT); } catch (Exception e) { Log.e("tmessages", e.toString()); } } createNotification(messageObject); } catch (Exception e) { e.printStackTrace(); } return START_STICKY; }
From source file:com.goftagram.telegram.messenger.MusicPlayerService.java
@SuppressLint("NewApi") @Override/* w ww .j a v a 2 s . c om*/ public int onStartCommand(Intent intent, int flags, int startId) { try { MessageObject messageObject = MediaController.getInstance().getPlayingMessageObject(); if (messageObject == null) { AndroidUtilities.runOnUIThread(new Runnable() { @Override public void run() { stopSelf(); } }); return START_STICKY; } if (supportLockScreenControls) { ComponentName remoteComponentName = new ComponentName(getApplicationContext(), MusicPlayerReceiver.class.getName()); try { if (remoteControlClient == null) { audioManager.registerMediaButtonEventReceiver(remoteComponentName); Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.setComponent(remoteComponentName); PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0); remoteControlClient = new RemoteControlClient(mediaPendingIntent); audioManager.registerRemoteControlClient(remoteControlClient); } remoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_STOP | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_NEXT); } catch (Exception e) { FileLog.e("tmessages", e); } } createNotification(messageObject); } catch (Exception e) { e.printStackTrace(); } return START_STICKY; }
From source file:co.codecrunch.musicplayerlite.manager.MusicPlayerService.java
@SuppressLint("NewApi") @Override//from w ww. j a v a 2 s . c o m public int onStartCommand(Intent intent, int flags, int startId) { try { SongDetail messageObject = MediaController.getInstance().getPlayingSongDetail(); if (messageObject == null) { MusicPlayerUtility.runOnUIThread(new Runnable() { @Override public void run() { stopSelf(); } }); return START_STICKY; } if (supportLockScreenControls) { ComponentName remoteComponentName = new ComponentName(getApplicationContext(), MusicPlayerReceiver.class.getName()); try { if (remoteControlClient == null) { audioManager.registerMediaButtonEventReceiver(remoteComponentName); Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.setComponent(remoteComponentName); PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0); remoteControlClient = new RemoteControlClient(mediaPendingIntent); audioManager.registerRemoteControlClient(remoteControlClient); } remoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_STOP | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_NEXT); } catch (Exception e) { Log.e("tmessages", e.toString()); } } createNotification(messageObject); } catch (Exception e) { e.printStackTrace(); } return START_STICKY; }