Ahsan Aasim
Ahsan Aasim

Reputation: 1317

Accessibilty Service is not started

I have an accessibility service that i use to track some app usages and let user know. Everything was working fine. Not sure suddenly my accessibility service stopped working. Its not even getting started. Permission is granted. But still the service doesn't start.

in manifest file

<service android:name=".Services.AutoStartReceiverService" android:enabled="true"></service>

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

Accessibility Service Code:

public class WindowChangeDetectingService extends AccessibilityService {

    private static final String TAG = "APPCHANGESERV";
    private SharedPreferences sharedpreferences;

    @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;

        if (Build.VERSION.SDK_INT >= 16)
            //Just in case this helps
            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()
                );

                ActivityInfo activityInfo = tryGetActivity(componentName);
                boolean isActivity = activityInfo != null;
                ApplicationInfo applicationInfo = null;
                ApplicationInfo savedApplicationInfo = null;
                sharedpreferences = getSharedPreferences("virtual_virus", Context.MODE_PRIVATE);
                if (isActivity) {
                    try {
                        applicationInfo = (getPackageManager().getApplicationInfo(componentName.getPackageName(), 0));
                        savedApplicationInfo = (getPackageManager().getApplicationInfo(sharedpreferences.getString("packageName", ""), 0));
                    } catch (PackageManager.NameNotFoundException e) {
                        e.printStackTrace();
                    }

                    if ((activityInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
                        Log.i(TAG, "system");
                    } else {

                        SharedPreferences.Editor editor = sharedpreferences.edit();

                        // For Closing
                        Utlis.clearAllNotification(getApplicationContext());

                        if (savedApplicationInfo != null) {
                            AppInfo.updateAppInfo(sharedpreferences.getString("packageName", ""), new Date(), false, savedApplicationInfo.category);
                        }


                        // For Opening
                        if (activityInfo != null) {

                            if (applicationInfo.category == ApplicationInfo.CATEGORY_VIDEO || applicationInfo.category == ApplicationInfo.CATEGORY_SOCIAL || applicationInfo.category == ApplicationInfo.CATEGORY_GAME) {
                                AppInfo.updateAppInfo(componentName.getPackageName(), new Date(), true, applicationInfo.category);

                                AppLimit appLimit = AppLimit.getAppLimit(componentName.getPackageName());
                                if (appLimit.getLimit() > 0) {
                                    AppInfo obj = AppInfo.getAppInfoByDateAndPackage(componentName.getPackageName(), new Date());
                                    Log.v(TAG, "obj.getAppUsage(): " + obj.getAppUsage());
                                    if ((appLimit.getLimit() * 60 * 1000) - obj.getAppUsage() > 0) {
                                        Utlis.scheduleNotification(getApplicationContext(), Utlis.getNotification(getApplicationContext(), componentName.getPackageName()), (int) Math.abs((appLimit.getLimit() * 60 * 1000) - obj.getAppUsage()));
                                    }
                                }

//                                else {
//                                    Utlis.scheduleNotification(getApplicationContext(), Utlis.getNotification(getApplicationContext(), componentName.getPackageName()), 1000);
//                                }
                            }
                        }

                        editor.putString("packageName", componentName.getPackageName());
                        editor.commit();

                        Log.i(TAG, componentName.getPackageName() + "  " + ApplicationInfo.FLAG_IS_GAME);
                    }

                }
            }
        }
    }

    private ActivityInfo tryGetActivity(ComponentName componentName) {
        try {
            return getPackageManager().getActivityInfo(componentName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            return null;
        }
    }

    @Override
    public void onInterrupt() {

    }
}

in string file:

<string name="accessibility_service_name">@string/app_name</string>
    <string name="accessibility_service_description">ভার্চুয়াল অ্যান্টিভাইরাস er je karone ei permission ti dorkar</string>

accessibilty service xml file:

<?xml version="1.0" encoding="utf-8"?><!-- These options MUST be specified here in order for the events to be received on first
 start in Android 4.1.1 -->
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:accessibilityEventTypes="typeWindowStateChanged"
    android:accessibilityFeedbackType="feedbackGeneric"
    android:accessibilityFlags="flagIncludeNotImportantViews"
    android:description="@string/accessibility_service_description"
    tools:ignore="UnusedAttribute" />

i am getting the accessibilty permission in my main activity like this:

accessPermissionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
                startActivityForResult(intent, 0);
            }
        });

And noticed it works perfectly on simulator.

Upvotes: 1

Views: 1219

Answers (1)

Ahsan Aasim
Ahsan Aasim

Reputation: 1317

Changed the accessesibilty service xml to below and it started working again

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:description="@string/accessibility_service_description"
    android:accessibilityEventTypes="typeWindowStateChanged"
    android:accessibilityFlags="flagDefault"
    android:accessibilityFeedbackType="feedbackSpoken"
    android:notificationTimeout="100"
    android:canRetrieveWindowContent="true"
    android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity"
    />

Upvotes: 1

Related Questions