1

I am developing lock application. I have to make the Home Button passive. I wrote an accessbility service.The Samsung j7 prime also works but does not work on phones in home button. I tried classic methods but it is not. Please help.

My Accessbility Service

    public class LockWindowAccessibilityService extends AccessibilityService {

    @Override
    protected boolean onKeyEvent(KeyEvent event) {

        LockScreen.getInstance().init(this);
        Log.i("#### KEY_CODE ####", String.valueOf(event.getKeyCode()));

        if (((LockApplication) getApplication()).lockScreenShow) {
            // disable home
            if (event.getKeyCode() == KeyEvent.KEYCODE_HOME || event.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER || event.getKeyCode() == KeyEvent.KEYCODE_APP_SWITCH) {
                return true;
            }
        }


        return super.onKeyEvent(event);
    }

    @Override
    protected boolean onGesture(int gestureId) {
        if(gestureId == GLOBAL_ACTION_HOME) {
            Log.d(TAG, "Gesture Home Pressed");
        }
        return super.onGesture(gestureId);
    }


    @Override
    public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
        Log.d("onAccessibilityEvent","onAccessibilityEvent");
        /*
        final int eventType = accessibilityEvent.getEventType();
        switch (eventType) {
            case AccessibilityEvent.TYPE_VIEW_CLICKED:

                break;
            case AccessibilityEvent.TYPE_VIEW_FOCUSED:

                break;
        }
        */
    }

    @Override
    public void onInterrupt() {

    }

}

My Accessbility.xml

    <?xml version="1.0" encoding="utf-8"?>
<accessibility-service
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:description="@string/permission_accessibility_setting_description"
    android:accessibilityEventTypes="typeAllMask"
    android:packageNames="com.abc"
    android:accessibilityFlags="flagRequestFilterKeyEvents|flagIncludeNotImportantViews|flagReportViewIds"
    android:accessibilityFeedbackType="feedbackAllMask"
    android:notificationTimeout="1000"
    android:canRetrieveWindowContent="true"
    android:canRequestFilterKeyEvents="true" />
Serkan
  • 13
  • 4

1 Answers1

0

The Home button displays the 'home' screen which is an app.

So the only way to 'block' the Home button is to replace the 'home' by your own app.

In your Manifest:

<activity
    android:name=".activities.MainActivity"
    ...>
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <!-- Set app as home -->
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.HOME" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

When your app will be installed, the system will ask the user which Home screen he wants to use. He'll have to select your app and check 'always' in order to your app to become the new Home screen. Otherwise your app will have a normal app behaviour. When your app is used as Home screen, pressing the Home button opens your app. As the app will already be displayed, nothing will happen and the Home button will look like it's disabled.

Eselfar
  • 3,759
  • 3
  • 23
  • 43
  • Hi @Eselfar, First of all, thank you for your interest. I tried this but there is a problem like this; screen is not locked and if the home button is pressed; my app is always open :( my application should only come if the screen is locked ? – Serkan Mar 16 '18 at 18:12
  • Ah ok, I misunderstood. So yeah my solution doesn't work :/ – Eselfar Mar 16 '18 at 18:14
  • 1
    When the screen is locked, the home button will be inactive.The home button will behave normally when the screen is unlocked. Actually the accessibility service is doing this but the home button does not work on the phones that are on the screen – Serkan Mar 16 '18 at 18:18
  • Do you hava an idea? – Serkan Mar 16 '18 at 19:01
  • Similar question: https://stackoverflow.com/questions/49494076/accessibilityservice-not-geting-key-event-on-softkey-back-home-button – Tamás Bolvári Sep 21 '18 at 23:19