6

我正在学习 AccessibilityService。我研究了什么是无障碍服务以及我们为什么使用它,无障碍服务有哪些优势。所以我试着做一个无障碍服务的演示,看看它是如何工作的,当一个电子邮件或任何whatsapp、微信消息来的时候,比它去的方法是什么,但我没有成功。当我开始这个项目时,我在 StartService(i); 线上得到了异常。因此,请查看此代码并告诉我犯了什么错误,您的评论和建议将对我和 AccessibilityService 初学者非常有帮助。

我使用的代码

在主活动中...

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);
    }
}

在 AccessibilityService 类中...

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();
    }

}

在清单文件中...

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

在 res/xml 文件夹中,即accessibilityservice.xml 文件...

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

我得到的logcat...

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
4

2 回答 2

8

您不能“启动”它们,AccessibilityService因为它们不像您可以控制其启动/停止的其他服务。而是在这里,Android 系统根据用户选择的设置来控制它们的启动和停止。

您可以做的最好的事情是启动辅助功能设置页面并让用户为您启用它:

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

希望能帮助到你!

于 2017-02-16T18:15:27.750 回答
3

您正在尝试创建自己的AccessibilityService. 这是不可能的,因为您无法持有android.permission.BIND_ACCESSIBILITY_SERVICE. 此外,这不是必需的,因为系统会在适当的时候启动您的服务(例如,用户已在“设置”中启用该服务)。

如果您的业务逻辑AccessibilityService还想在其他地方使用,请将其移动到可以由AccessibilityService您的代码和您的其他代码使用的某个中心位置。

于 2014-06-28T20:37:17.430 回答