There is a task: by pressing a certain key on the keyboard (or shortcut keys) to launch a specific action / application. From the documentation I realized that pressing ordinary keys can be processed only from the Activity , and from the service only the Media Button , but at the same time it is necessary to preserve the functionality of the keys for other applications (for example, when Play pressed, to start playing).
In the manifest:
<receiver android:name=".RemoteControlReceiver"> <intent-filter> <action android:name="android.intent.action.MEDIA_BUTTON" /> </intent-filter> </receiver> In the onCreate() service I register the receiver:
am = (AudioManager) getSystemService(AUDIO_SERVICE); // Start listening for button presses am.registerMediaButtonEventReceiver(new ComponentName(this, RemoteControlReceiver.class)); IntentFilter mediaButton = new IntentFilter(Intent.ACTION_MEDIA_BUTTON); registerReceiver(remoteControlReceiver, mediaButton); Class RemoteControlReceiver :
public void onReceive(Context context, Intent intent) { KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); Log.d(LOG_TAG, "RemoteControlReceiver: received key event = " + event.getKeyCode()); } This code once intercepted pressing Play , after this pressing is intercepted by the player. Tell me how to implement the idea!