我正在学习 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