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!

    1 answer 1

    I found the solution myself, maybe it would be useful for someone ... The Media Button method did not work, so I went along an alternative path through the AccessibilityService . Implementation:

    In the manifest, added a description of the service:

     <service android:name=".MyAccessibilityService" android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.accessibilityservice.AccessibilityService" /> </intent-filter> <meta-data android:name="android.accessibilityservice" android:resource="@xml/accessibilityservice" /> </service> 

    xml with service parameters:

     <accessibility-service xmlns:android="http://schemas.android.com/apk/res/android" android:accessibilityEventTypes="typeAllMask" android:accessibilityFeedbackType="feedbackGeneric" android:accessibilityFlags="flagIncludeNotImportantViews|flagRequestFilterKeyEvents" android:settingsActivity="ru.atic.autodroid.StartScreen" android:canRetrieveWindowContent="true" android:canRequestTouchExplorationMode="true" android:canRequestFilterKeyEvents="true" /> 

    Well, the service itself:

     public class MyAccessibilityService extends android.accessibilityservice.AccessibilityService { final String LOG_TAG = "mLog"; @Override protected boolean onKeyEvent(KeyEvent event) { Log.d(LOG_TAG, "MyAccessibilityService: onKeyEvent: action = " + event.getAction() + "; key code = " + event.getKeyCode() + "; scan code = " + event.getScanCode() + "; meta state = " + event.getMetaState() + "; key = " + event.getNumber()); int keyCode = event.getKeyCode(); switch (keyCode) { case 8: Intent intent8 = getPackageManager().getLaunchIntentForPackage("com.bambuna.podcastaddict"); intent8.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent8); break; case 9: Intent intent9 = getPackageManager().getLaunchIntentForPackage("com.pasha.kissfm"); intent9.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent9); break; case 10: Intent intent10 = getPackageManager().getLaunchIntentForPackage("com.google.android.apps.maps"); intent10.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent10); break; } return super.onKeyEvent(event); } 

    By pressing the keyboard "1", "2", "3" starts the corresponding applications. Of course, the service requires the appropriate permission from the user, but this option suits me.

    • Of course, the service requires an appropriate permission from the user for operation - this is understandable, otherwise the implementation of a trojan keylogger would be a trifling matter. - D-side