Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessibility service is not started

I want to log all toasts events in android ics (4.0.3), but I was unable to log any system event. Service is just not started!

According to this question: onAccessibilityEvent(AccessibilityEvent event) not intercepting notification

MyAccessibilityService.java

package com.test.toasts2;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.app.Notification;
import android.os.Parcelable;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.widget.Toast;

public class MyAccessibilityService extends AccessibilityService {


    public static final String TAG = "volumeMaster";

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event)
    {
        Log.v(TAG, "***** onAccessibilityEvent");
        Toast.makeText(getApplicationContext(), "Got event from: " + event.getPackageName(), Toast.LENGTH_LONG).show();
    }

    @Override
    public void onInterrupt()
    {
        Log.v(TAG, "***** onInterrupt");
    }

    @Override
    public void onServiceConnected()
    {
        Log.v(TAG, "***** onServiceConnected");       


        AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
        info.notificationTimeout = 100;
        info.feedbackType = AccessibilityEvent.TYPES_ALL_MASK;
        setServiceInfo(info);

    }
}

Toast2Activity.java

package com.test.toasts2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class Toast2Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent i = new Intent(this, MyAccessibilityService.class);
        startService(i);
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.toasts2"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
<service android:name=".MyAccessibilityService" android:label="@string/app_name" android:enabled="true" android:exported="false">
        <intent-filter>
            <action android:name="android.accessibilityservice.AccessibilityService" />
        </intent-filter>

</service>

        <activity
            android:name=".Toast2Activity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

I included activity tag in manifest (to start service on app start), and also tried to remove. Nothing changed. Service is just not started. I don't get notification in log cat (Log.v in onServiceConected).

I am compiling this as normal app (not system app), android 4.0.3. Am I doing something wrong?

Attached project (may be mistake is somewhere else, or maybe I am compiling it wrong): https://dl.dropbox.com/u/1928109/toast2.zip

like image 299
POMATu Avatar asked Jun 18 '12 17:06

POMATu


People also ask

How do I fix accessibility not working?

Launch the "Settings" app from the All Apps screen, then choose "Accessibility." Make sure the options you require are all enabled. Select "Enhance Web Accessibility," for example, to allow Google websites to install browser scripts to aid accessibility.

How do I fix Android accessibility settings to turn off automatically?

On your device, open Settings > Accessibility. Scroll until you find Accountable2You. Tap on Accountable2You. Toggle Accessibility to Off and then On again (it may show as on but still be disabled - this step will reset it).

What are accessibility services in Android?

What is an Accessibility Service? An Accessibility Service assists users with disabilities in using Android devices and apps. It is a long-running privileged service that helps users process information on the screen and lets them to interact meaningfully with a device.


2 Answers

You need to enable the accessibility service from the system settings menu:

  • Install your app on the testing device.

  • With the app installed on the device, change the following setting on the device:

    Home Screen > System Settings > Accessibility > Accessibility Services > Toast2 > Change from Off to On,

    where Toast2 is your app name.

  • Then run your app.

AFAIK there is no method to set accessibility programmatically to On mode from within your app. As a workaround you can prompt the user to change the setting and if they agree by clicking a button, launch the accessibility system settings like so:

Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
startActivityForResult(intent, 0);
like image 121
onosendai Avatar answered Sep 19 '22 17:09

onosendai


As a complementary answer to the comments, one more thing worth noting is that you have to declare permission - android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" in your service, or else there will be no switch in setting->accessibility.

like image 22
candy Avatar answered Sep 19 '22 17:09

candy