1

Next code stopped working in Android 8.1 when smartphone is locked (display is black)

it only works if phone is unlocked

In Android 8.0 I remember next code was working always

<service
    android:label="@string/accessibility_service_name"
    android:name=".WindowChangeDetectingService"
    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>

My AccessibilityService class

public class WindowChangeDetectingService extends AccessibilityService {

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();

        //Configure these here for compatibility with API 13 and below.
        AccessibilityServiceInfo config = new AccessibilityServiceInfo();
        config.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
        config.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
        config.flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;

        setServiceInfo(config);
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
            if (event.getPackageName() != null && event.getClassName() != null) {
                ComponentName componentName = new ComponentName(
                        event.getPackageName().toString(),
                        event.getClassName().toString()
                );
                Log.i("Component", componentName.flattenToShortString() + " " + componentName.getShortClassName());
            }
        }
    }

UPDATE

Cause such feature I need only for my phone I found next solution:

Options / Security & Location / Screen lock / None

(Select None)

user155
  • 775
  • 1
  • 7
  • 25

1 Answers1

0

Perhaps your Service is being killed by OS due to Doze mode or Standby.

In Doze mode, the system attempts to conserve battery by restricting apps' access to network and CPU-intensive services.

Now as solution for this you can check periodically (say by 2 hours) if your service is killed. Implement periodic task by WorkManager. And restart service if killed.

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212