Skip to content Skip to sidebar Skip to footer

How To Capture Key Events Inside A Service?

I want to be able to capture key events inside a service I am writing. I can do this inside an activity without problems, but all my attemps to get this working in a service have f

Solution 1:

A Service has no UI so it doesn't receive any input from the User.

Now if you had an activity that managed the service then you could make the service do something special when the back key was pressed while in your activity.

Solution 2:

You can capture hardware key button events by using accessibility service, only you need to enable the service by going to accessibility settings after installing the app. Here is the code

publicclassAccessiblityServiceextendsAccessibilityService {

        @OverrideprotectedvoidonServiceConnected() {
            super.onServiceConnected();
            Log.d(TAG, "service is connected");
        }

        @OverridepublicvoidonAccessibilityEvent(AccessibilityEvent accessibilityEvent) {

            Log.d(TAG, "onAccessibiltyEvent" + accessibilityEvent.toString());

        }

        @OverridepublicvoidonInterrupt() {

        }
    // here you can intercept the keyevent@OverrideprotectedbooleanonKeyEvent(KeyEvent event) {
            returnhandleKeyEvent(event);
        }

         privatebooleanhandleKeyEvent(KeyEvent event) {
            int action = event.getAction();
            int keyCode = event.getKeyCode();
            if (action == KeyEvent.ACTION_DOWN) {
                switch (keyCode) {
                    caseKeyEvent.KEYCODE_VOLUME_DOWN:
                        //do somethingreturntrue;

                    caseKeyEvent.KEYCODE_VOLUME_UP: {
                        //do somethingreturntrue;
                    }
                }
            }
            returnfalse;
        }

} 

manifest.xml

<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"package="com.mypackagename"><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"tools:ignore="GoogleAppIndexingWarning"><serviceandroid:name=".AccessiblityService"android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"><intent-filter><actionandroid:name="android.accessibilityservice.AccessibilityService" /></intent-filter><meta-dataandroid:name="android.accessibilityservice"android:resource="@xml/accessibility_service" /></service><activityandroid:name=".MainActivity"><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

accessibility_service.xml //create it in "xml" directory

<?xml version="1.0" encoding="utf-8"?><accessibility-servicexmlns:android="http://schemas.android.com/apk/res/android"android:canRequestFilterKeyEvents="true"android:accessibilityFlags="flagRequestFilterKeyEvents"/>

Solution 3:

Yes, Android's activities only receives KeyEvents when they have focus.

The only way to "globally" capture a back button press is creating an InputMethod so you can intercept hard key events. Remember that using your own InputMethod will not allow you to use custom keyboards like Swiftkey for instance.

Did you try to reach HTC on this issue?

Solution 4:

You can broadcast the keyEvent from framework and handle the broadcast in your Service. But you need change the code in framework for this.

Solution 5:

If you just want to capture the event of changing volume regardless of volume button is pressed or not, this answer would helpful.

any way to detect volume key presses or volume changes with android service?

Post a Comment for "How To Capture Key Events Inside A Service?"