Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use AccessibilityService?

I am studying AccessibilityService. I studied what is Accesibility Service and why we use it, what are advantages of Accessibility Service. So I tried to make a demo of Accessibility service to see how it works, that when a Email or any whatsapp, wechat message comes than in which method it goes But I didn't get success. As I started this project I get Exception on line StartService(i); So kindly look over this code and tell what mistake I am making, Your comment and suggestion will be so helpful for me and for AccessibilityService beginners.

The code I used

In the MainActivity...

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent i = new Intent(MainActivity.this, MyService.class);
        startService(i);
    }
}

In the AccessibilityService class...

public class MyService extends AccessibilityService{


    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        // TODO Auto-generated method stub
        Log.i("Accessibility", "onAccessibilityEvent");
        Toast.makeText(getApplicationContext(), "onAccessibilityEvent", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onInterrupt() {
        // TODO Auto-generated method stub
        Log.i("Interrupt", "Interrupt");
        Toast.makeText(getApplicationContext(), "onInterrupt", Toast.LENGTH_SHORT).show();

    }

    @Override
    protected void onServiceConnected() {
        // TODO Auto-generated method stub
        super.onServiceConnected();
        Log.i("Service", "Connected");
        Toast.makeText(getApplicationContext(), "onServiceConnected", Toast.LENGTH_SHORT).show();
    }

}

In the Manifest File...

<uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" />
<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.notifications.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <service
        android:name="MyService"
        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>

    </application>

In the res/xml folder i.e accessibilityservice.xml file...

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeNotificationStateChanged"
    android:accessibilityFeedbackType="feedbackAllMask"
    android:notificationTimeout="100" />

The logcat I got...

06-29 01:58:40.798: E/AndroidRuntime(21707): FATAL EXCEPTION: main
06-29 01:58:40.798: E/AndroidRuntime(21707): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.notifications/com.example.notifications.MainActivity}: java.lang.SecurityException: Not allowed to start service Intent { cmp=com.example.notifications/.MyService } without permission android.permission.BIND_ACCESSIBILITY_SERVICE
06-29 01:58:40.798: E/AndroidRuntime(21707):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
06-29 01:58:40.798: E/AndroidRuntime(21707):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
06-29 01:58:40.798: E/AndroidRuntime(21707):    at android.app.ActivityThread.access$1500(ActivityThread.java:121)
06-29 01:58:40.798: E/AndroidRuntime(21707):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
06-29 01:58:40.798: E/AndroidRuntime(21707):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-29 01:58:40.798: E/AndroidRuntime(21707):    at android.os.Looper.loop(Looper.java:130)
06-29 01:58:40.798: E/AndroidRuntime(21707):    at android.app.ActivityThread.main(ActivityThread.java:3701)
06-29 01:58:40.798: E/AndroidRuntime(21707):    at java.lang.reflect.Method.invokeNative(Native Method)
06-29 01:58:40.798: E/AndroidRuntime(21707):    at java.lang.reflect.Method.invoke(Method.java:507)
06-29 01:58:40.798: E/AndroidRuntime(21707):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
06-29 01:58:40.798: E/AndroidRuntime(21707):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
06-29 01:58:40.798: E/AndroidRuntime(21707):    at dalvik.system.NativeStart.main(Native Method)
06-29 01:58:40.798: E/AndroidRuntime(21707): Caused by: java.lang.SecurityException: Not allowed to start service Intent { cmp=com.example.notifications/.MyService } without permission android.permission.BIND_ACCESSIBILITY_SERVICE
06-29 01:58:40.798: E/AndroidRuntime(21707):    at android.app.ContextImpl.startService(ContextImpl.java:867)
06-29 01:58:40.798: E/AndroidRuntime(21707):    at android.content.ContextWrapper.startService(ContextWrapper.java:336)
06-29 01:58:40.798: E/AndroidRuntime(21707):    at com.example.notifications.MainActivity.onCreate(MainActivity.java:15)
06-29 01:58:40.798: E/AndroidRuntime(21707):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-29 01:58:40.798: E/AndroidRuntime(21707):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
06-29 01:58:40.798: E/AndroidRuntime(21707):    ... 11 more
like image 362
nawaab saab Avatar asked Jun 28 '14 20:06

nawaab saab


2 Answers

You can't "start" the AccessibilityService as they are not like other services where you can control their start/stop. Rather here the Android system controls starting and stopping them based on the settings the user has selected.

The best you can do is launch the Accessibility settings page and get the user to enable it for you:

Intent openSettings = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
openSettings.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(openSettings);

Hope it helps!

like image 185
Ankit Aggarwal Avatar answered Sep 16 '22 17:09

Ankit Aggarwal


You are attempting to start your own AccessibilityService. That is not possible, as you cannot hold android.permission.BIND_ACCESSIBILITY_SERVICE. Moreover, it should not be necessary, as the system will start your service when appropriate (e.g., the user has enabled the service in Settings).

If you have business logic in your AccessibilityService that you also want to use elsewhere, move it to some central spot that can be used by both the AccessibilityService and your other code.

like image 24
CommonsWare Avatar answered Sep 17 '22 17:09

CommonsWare