7

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 failed. The key I wanted to capture (globally) is the BACK button, but if this is not possible any (hardware) button a HTC Desire offers would be OK.

user388759
  • 93
  • 1
  • 1
  • 6
  • Why do you want to capture it in a `Service`? – Macarse Jul 12 '10 at 15:55
  • First: I am new to android programming and I thought an activity only receives KeyEvents when it has focus. If this is not the case I do not strictly need a service. What I want to do is to write an application that can deactivate the screen lock that was activated by the proximity sensor when a call is made. This is necessary because the HTC Desire sometimes 'forgets' to re-enable the screen properly I wanted to do this when any button is pressed (to be able to hang up without having to remove the battery ;) ). – user388759 Jul 12 '10 at 16:27

5 Answers5

3

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

    public class AccessiblityService extends AccessibilityService {

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

        @Override
        public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {

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

        }

        @Override
        public void onInterrupt() {

        }
    // here you can intercept the keyevent
        @Override
        protected boolean onKeyEvent(KeyEvent event) {
            return handleKeyEvent(event);
        }

         private boolean handleKeyEvent(KeyEvent event) {
            int action = event.getAction();
            int keyCode = event.getKeyCode();
            if (action == KeyEvent.ACTION_DOWN) {
                switch (keyCode) {
                    case KeyEvent.KEYCODE_VOLUME_DOWN:
                        //do something
                        return true;

                    case KeyEvent.KEYCODE_VOLUME_UP: {
                        //do something
                        return true;
                    }
                }
            }
            return false;
        }

} 

manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.mypackagename">

    <application
        android: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">
        <service
            android:name=".AccessiblityService"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
            <intent-filter>
                <action android:name="android.accessibilityservice.AccessibilityService" />
            </intent-filter>
            <meta-data
                android:name="android.accessibilityservice"
                android:resource="@xml/accessibility_service" />
        </service>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android: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-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:canRequestFilterKeyEvents="true"
    android:accessibilityFlags="flagRequestFilterKeyEvents"/>
Mushahid Gillani
  • 723
  • 7
  • 19
2

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.

Robby Pond
  • 73,164
  • 16
  • 126
  • 119
1

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?

Macarse
  • 91,829
  • 44
  • 175
  • 230
  • It's not my device (I have an Samsung Galaxy I9000 -- which has it's own problems ;) ) but that one of a friend of mine. He searched some forums, found some people having the same problem but no solution so far (I don't know if he contacted HTC). But after Android being open so that we should be able to fix the problems we encounter (so I thought) the idea was to do just that. ;) – user388759 Jul 12 '10 at 17:56
1

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

0

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?

Community
  • 1
  • 1
mvmanh
  • 151
  • 2
  • 4