onKeyDown in a service? (Global Hot Keys)

前端 未结 2 1071
长发绾君心
长发绾君心 2020-12-01 09:46

What I\'m basically trying to achieve is custom global hot keys or in other words \'things will happen when I push certain buttons no matter which program is currently on to

相关标签:
2条回答
  • 2020-12-01 10:03

    You cannot do this :)

    0 讨论(0)
  • 2020-12-01 10:07

    This requires Lollipop (v5.0/API 21) or higher

    My goal was to adjust system volume from a Service, so this will only listen for the rocker. Any action can be taken on press though.

    It will override volume key action, so using it globally may not be desired.

    public class VolumeKeyController {
    
        private MediaSessionCompat mMediaSession;
        private final Context mContext;
    
        public VolumeKeyController(Context context) {
            mContext = context;
        }
    
        private void createMediaSession() {
            mMediaSession = new MediaSessionCompat(mContext, KeyUtil.log);
    
            mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
                    MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
            mMediaSession.setPlaybackState(new Builder()
                    .setState(PlaybackStateCompat.STATE_PLAYING, 0, 0)
                    .build());
            mMediaSession.setPlaybackToRemote(getVolumeProvider());
            mMediaSession.setActive(true);
        }
    
        private VolumeProviderCompat getVolumeProvider() {
            final AudioManager audio = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    
            int STREAM_TYPE = AudioManager.STREAM_MUSIC;
            int currentVolume = audio.getStreamVolume(STREAM_TYPE);
            int maxVolume = audio.getStreamMaxVolume(STREAM_TYPE);
    
            return new VolumeProviderCompat(VolumeProviderCompat.VOLUME_CONTROL_RELATIVE, maxVolume, currentVolume) {
                @Override
                public void onAdjustVolume(int direction) {
                    // Volume Up = AudioManager.ADJUST_LOWER, Volume Down = AudioManager.ADJUST_LOWER, Release = AudioManager.ADJUST_SAME
                    // Replace with your action, if you don't want to adjust system volume
                    if (direction == AudioManager.ADJUST_RAISE) {
                        audio.adjustStreamVolume(STREAM_TYPE,
                                AudioManager.ADJUST_RAISE, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
                    }
                    else if (direction == AudioManager.ADJUST_LOWER) {
                        audio.adjustStreamVolume(STREAM_TYPE,
                                AudioManager.ADJUST_LOWER, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
                    }
                    setCurrentVolume(audio.getStreamVolume(STREAM_TYPE));
                }
            };
        }
    
        // Call when control needed, add a call to constructor if needed immediately
        public void setActive(boolean active) {
            if (mMediaSession != null) {
                mMediaSession.setActive(active);
                return;
            }
            createMediaSession();
        }
    
        // Call from Service's onDestroy method
        public void destroy() {
            if (mMediaSession != null) {
                mMediaSession.release();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题