根据给定的条件,文本不会改变

问题描述 投票:0回答:1

在我的程序中,我有一个开关,一个文本视图和一个可访问性服务运行。

当开关打开时,第二个屏幕打开,我需要打开我的服务,这样,第一个屏幕中的文本应该变为ON(仅当启用了服务时,文本才会变为ON)。

但问题是,只要我打开开关,但在我从第二个屏幕启用我的服务之前,第一个屏幕中的文本就会变为ON。

这是我的交换机代码:

 private void setSwitchAndText(){
        boolean ifON;
        ifON = isAccessibilitySettingsOn(getApplicationContext());
        if(ifON) {
            mySwitch.setChecked(true);
        } else {
            mySwitch.setChecked(false);

        }
        myTextview.setText(ifON ? R.string.on : R.string.off);
    }

这就是我检查服务是否启用的方式:

private boolean isAccessibilitySettingsOn(Context mContext) {
        int accessibilityEnabled = 0;
        final String service = getPackageName() + "/" + myService.class.getCanonicalName();
        boolean accessibilityFound = false;

        try {
            accessibilityEnabled = Settings.Secure.getInt(this.getContentResolver(),android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
            Log.d(LOGTAG, "ACCESSIBILITY: " + accessibilityEnabled);
        } catch (Settings.SettingNotFoundException e) {
            Log.d(LOGTAG, "Error finding setting, default accessibility to not found: " + e.getMessage());
        }
        TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');

        if (accessibilityEnabled == 1) {
            Log.d(TAG, "***ACCESSIBILITY IS ENABLED*** -----------------");
            String settingValue = Settings.Secure.getString(
                    mContext.getApplicationContext().getContentResolver(),
                    Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
            if (settingValue != null) {
                mStringColonSplitter.setString(settingValue);
                while (mStringColonSplitter.hasNext()) {
                    String accessibilityService = mStringColonSplitter.next();

                    Log.d(TAG, "-------------- > accessibilityService :: " + accessibilityService + " " + service);
                    if (accessibilityService.equalsIgnoreCase(service)) {
                        Log.d(TAG, "We've found the correct setting - accessibility is switched on!");
                        return true;
                    }
                }
            }
        } else {
            Log.v(TAG, "***ACCESSIBILITY IS DISABLED***");
        }

        return accessibilityFound;
    }

这是onCreate中的相关代码:

myTextview = (TextView) findViewById(R.id.my_text);
    mySwitch = (Switch) findViewById(R.id.my_switch);
    mySwitch.setChecked(false);
    mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            myTextview.setText(isChecked ? R.string.on : R.string.off);
            if (isChecked) {
                askToStartAccessibilityService(getResources().getString(R.string.on));
            } else {
                askToStartAccessibilityService(getResources().getString(R.string.off));
            }
        }
    });
android accessibilityservice
1个回答
0
投票

您检测活动可访问性服务的方式非常激烈,并且没有理由这样做。请尝试使用此代码:

AccessibilityManager accessibilityManager = (AccessibilityManager) getSystemService(Context.ACCESSIBILITY_SERVICE);

List<AccessibilityServiceInfo> serviceInfoList = accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_SPOKEN);

return (serviceInfoList.size() > 0);
© www.soinside.com 2019 - 2024. All rights reserved.