1

как можно переопределить поведение кнопки домой на android из кода Activity? Пробовал onKeyDown() и onKeyUp(), но не работает! Спасибо заранее

1

1 ответ 1

1

Я сделал с помощью AccessibilityService.

Сам сервис:

@Override
    protected boolean onKeyEvent(KeyEvent event) {
        int keyCode = event.getKeyCode();
        switch (keyCode){
            case 3:
                Toast.makeText(getApplicationContext(), "HOME", Toast.LENGTH_SHORT).show();
                return  true;
        }
        return super.onKeyEvent(event);
    }

Файл accessibilityservice.xml в папке res/xml/:

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeAllMask"
    android:accessibilityFeedbackType="feedbackGeneric"
    android:accessibilityFlags="flagIncludeNotImportantViews|flagRequestFilterKeyEvents"
    android:settingsActivity=".MainActivity"
    android:canRetrieveWindowContent="true"
    android:canRequestTouchExplorationMode="true"
    android:canRequestFilterKeyEvents="true"
    />

Часть AndroidManifest.xml:

        <service
            android:name=".MyAS"
            android:label="Кнопки"
            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/accessibilityservice"/>
        </service>

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.