64

Each time I start a new debug instance, my accessibility service resets to the disabled state.

Is there any way to keep it enabled across successive debug runs (as it is quite long & boring to enable it each time in order to debug the service)?

I have the same behavior on real device and emulators.
There is no exception in the service, I tried event with no code in the event handler.

There are suspicious lines in my logs:

10:47:32.801 31669-31669/? E/AffinityControl: AffinityControl: registerfunction enter
10:47:32.821 3650-3690/? I/ActivityManager: Force stopping com.test.testaccessibilityservice appid=10241 user=0: from pid 31669
10:47:32.821 3650-3690/? I/ActivityManager: Killing 31271:com.test.testaccessibilityservice/u0a241 (adj 1): stop com.test.testaccessibilityservice cause from pid     
10:47:32.821 3650-3690/? W/ActivityManager: Scheduling restart of crashed service com.test.testaccessibilityservice/.MyAccessibilityService in 1000ms
10:47:32.821 3650-3690/? I/ActivityManager:   Force stopping service ServiceRecord{3f5e1fc4 u0 com.test.testaccessibilityservice/.MyAccessibilityService}

So the service is force stopped and never restarted.

Notes:

  • If I reboot the phone, the service is started.
  • I have the same behavior with the ApiDemos sample and ClockBackService (QueryBackService too):

    18:07:15.871 3523-4251/? I/ActivityManager: Force stopping com.example.android.apis appid=10242 user=0: from pid 19382
    18:07:15.871 3523-4251/? I/ActivityManager: Killing 16542:com.example.android.apis/u0a242 (adj 1): stop com.example.android.apis cause from pid 19382
    18:07:15.871 3523-4251/? W/ActivityManager: Scheduling restart of crashed service com.example.android.apis/.accessibility.ClockBackService in 1000ms
    18:07:15.871 3523-4251/? I/ActivityManager:   Force finishing activity 3 ActivityRecord{2f907c7b u0 com.example.android.apis/.ApiDemos t8248}
    18:07:15.881 3523-4251/? I/ActivityManager:   Force finishing activity 3 ActivityRecord{190ca05c u0 com.example.android.apis/.ApiDemos t8248}
    18:07:15.881 3523-4251/? I/ActivityManager:   Force finishing activity 3 ActivityRecord{27ada6e8 u0 com.example.android.apis/.accessibility.ClockBackActivity t8248}
    18:07:15.881 3523-4251/? I/ActivityManager:   Force finishing activity 3 ActivityRecord{51f4c32 u0 com.android.settings/.Settings$AccessibilitySettingsActivity t8248}
    18:07:15.881 3523-4251/? I/ActivityManager:   Force stopping service ServiceRecord{113bf024 u0 com.example.android.apis/.accessibility.ClockBackService}
    18:07:15.891 19382-19382/? D/AndroidRuntime: Shutting down VM
    

I've tried to return START_STICKY by overriding onStartCommand without any change.

It is very closed to this old unanswered question How to debug accessibility service?, but in my case, the service appears disabled, and I do not need to stop it and start it again.

I filled out this bug report on AOSP.

Tom Aranda
  • 5,919
  • 11
  • 35
  • 51
Jérémy Reynaud
  • 3,020
  • 1
  • 24
  • 36
  • I do accessibility service development all the time, and have never seen this behavior. Tested across Droid Ultra, Nexus 5, 7, 9, Droid Turbo, and all Samsung Galaxy tab variants. Your service may be exiting improperly. Have you tried looking at completely unflitered LogCat logs and looking for crash reports/exceptions? Service error handling and exiting isn't quite as clean as general application development. – MobA11y Dec 30 '15 at 05:52
  • I add some logs and update my question with some details. Do you have the same logs (killing & scheduling restart)? – Jérémy Reynaud Jan 06 '16 at 18:18
  • The second occurance of "Force stopping" (last line in your log) seems suspicious to me. – MobA11y Jan 06 '16 at 19:39
  • I have the same line (and same problem) with ApiDemos & ClockBackService (I add the logs). Just to be sure, do you have the same lines in your debug console: `Force stopping package: com.example.android.apis DEVICE SHELL COMMAND: am force-stop com.example.android.apis Launching application: com.example.android.apis/com.example.android.apis.ApiDemos. DEVICE SHELL COMMAND: am start -D -n "com.example.android.apis/com.example.android.apis.ApiDemos" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER`? – Jérémy Reynaud Jan 07 '16 at 17:59
  • What happens if you go into `Settings -> Accessibility`? Do you see the services enabled? If you enable them manually, can you debug afterwards? – eoinzy Feb 02 '16 at 17:54
  • In Accessibility, the service is disabled. If I enable it, I can debug it yes. – Jérémy Reynaud Feb 03 '16 at 07:28
  • After some weeks of debugging, I can tell the behaviour are not consistent across devices/emulators: on some the service is disabled and you can enable it and debug it, on some others the service "seems" enabled (it is marked as Enabled in Accessibility service & my code to check it returns that it is enabled) BUT the service is not really started. In this case disabling and enabling it do not work everytime (fails most of time) and the only viable solution is uninstalling and rebooting the device (worst case ever - debugging is hell here) – Jérémy Reynaud Feb 11 '16 at 12:44
  • I filled out a bug report on ASOP: https://code.google.com/p/android/issues/detail?id=201290. Feel free to stare it and add your own case descriptions. – Jérémy Reynaud Feb 19 '16 at 08:23
  • It happens with me also when app removed from memory, on xiaomi note 3, but hike app's accessibility service is running in both cases – UMESH0492 Jul 17 '16 at 19:39
  • have you tried starting the `AccessibilityService` with the `Context.BIND_AUTO_CREATE` flag? would step into `AffinityControl` to see what `registerfunction enter` does. hard to tell, while not having seen the code of the `AccessibilityService` (eg. how it is being started?). – Martin Zeitler Oct 28 '17 at 06:10
  • On devices where you can replicate this, are any other Accessibility Services enabled? If they are, disable them and see if the behaviour changes for you. – brandall Feb 05 '18 at 17:17
  • I can't replicate it right now but usually I didn't have any other Accessibility Services enabled. – Jérémy Reynaud Feb 05 '18 at 18:47
  • Can you post your accessibility service code? at least huawei and some other chinese devices have things where a stopped app that is not whitelisted will stop all services if it is stopped in certain ways. really annoying. – Lassi Kinnunen Mar 01 '18 at 07:22
  • Code won't help, nothing fancy here, you can use some Accessibility Service sample code and face the same issue: https://developer.android.com/training/accessibility/service.html. I just think it is a bug and it as been considered low priority and closed as "Wont-Fix": https://issuetracker.google.com/issues/37082339. I tried (long time ago because now I've removed all accessibility services from my apps due to Google policy changes) with real devices and emulators with always this same issue. – Jérémy Reynaud Mar 01 '18 at 10:39

1 Answers1

1

This might go some way to explain and mitigate your problem (but not what causes your Force stop).

After Android 3.1 " the system sets FLAG_EXCLUDE_STOPPED_PACKAGES on all broadcast intents." So after 3.1 , all apps are stopped on boot. Why ?. For security reasons.

There are RULES to turn the flag off FLAG_EXCLUDE_STOPPED_PACKAGES.

(1) If the app get's a Force stop from settings OR unresponsive app button, the flag is set.

(2) Your app needs to be in Phone Storage, NOT external storage (e.g. sdcard) otherwise the flag set. The BOOT_COMPLETE is sent before external storage is mounted. So, if app is installed to external storage it won't receive BOOT_COMPLETE broadcast message.

(3) If the application has never been run, the flag is set (never is relative to current boot state ;O) NEVER means in THIS boot OR you invalidated the flag in the last boot state).

A quick way (scripted if you like) to re-enable your service after Force stop assuming BOOT_COMPLETED receiver (I'm guessing you have this, because your fine after reboot):

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED

See launchcontrols

Jon Goodwin
  • 9,053
  • 5
  • 35
  • 54