skip to Main Content

When I switched migrating from react-native with native modules only to kotlin after ‘run application’ (shift+f10) my application removes my accessibility permission. I wonder is there way to always enable it?

Changing the code seems wrong, because it can be published to the end user.
I opened android studio and changed configuration.
I added ‘run external tools’ to Before launch option.
with adb shell settings put secure enabled_accessibility_services myService

but after permission was granted I could not ‘run application’ because of ‘error running app device is already activating’.

I could fix it using adb kill-server.

But for that action I need manually type that command. Is there a way to create a background task that could set this permission.

Using both commands does not help me restart my program and see the new changes, because I get same error.

2

Answers


  1. Chosen as BEST ANSWER

    first problem with error 'running device' connected to adb I fixed on linux this helped me. I used

    adb kill-server
    sudo cp ~/Android/Sdk/platform-tools/adb /usr/bin/adb
    sudo chmod +x /usr/bin/adb
    adb start-server
    

    Second problem solved. I created a simple sh script that listen custom activity event and enable accessibility service even after restart. Only need to run it once in background.

    #!/bin/bash
    PACKAGE_NAME="com.mypackage"
    
    adb logcat | grep --line-buffered "ActivityManager: Start.*$PACKAGE_NAME" | while read -r line
    do
        echo "Detected activity start for $PACKAGE_NAME"
        adb shell settings put secure enabled_accessibility_services myPacakge
    done
    

  2. Try this

    1. Create a Background Service
    2. Declare the Service in the Manifest
    3. Enable the Service
    4. Grant Accessibility Permissions
    5. Use ADB to Enable the Service
    6. Restart the App

    Here is the code for the same

    SERVICE FILE

    class MyAccessibilityService : AccessibilityService() {
    
        override fun onAccessibilityEvent(event: AccessibilityEvent?) {
            // Handle accessibility events here
        }
    
        override fun onInterrupt() {
            // Handle interruptions here
        }
    }
    

    MANIFEST FILE

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.your.package.name">
    
        <uses-permission android:name="android:permission.BIND_ACCESSIBILITY_SERVICE" />
    
        <service
            android:name=".MyAccessibilityService"
            android:enabled="true"   
    
            android:label="My Accessibility Service"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
            <intent-filter>
                <action android:name="android.accessibilityservice.AccessibilityService" />  
             
            </intent-filter>   
    
        </service>
    </manifest>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search