辅助功能服务未启动

时间:2018-07-04 12:51:16

标签: android android-service accessibilityservice android-accessibility accessibility-api

我有一个可访问性服务,可用来跟踪一些应用程序的使用情况并告知用户。一切都很好。不确定我的辅助功能突然停止工作了。它甚至还没有开始。授予权限。但是服务仍然无法启动。

清单文件中

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

无障碍服务代码:

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() {

    }
}

在字符串文件中:

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

可访问性服务xml文件:

<?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" />

我正在这样的主要活动中获得访问权限:

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

并且注意到它在模拟器上可以完美运行。

1 个答案:

答案 0 :(得分:0)

将可访问性服务xml更改为以下内容,并重新开始工作

<?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"
    />