我正在学习AccessibilityService。我已经学习了什么是Accessibility Service以及为什么我们要使用它,以及Accessibility Service的优点。因此,我尝试制作一个Accessibility service的演示,以了解它的工作原理,即当收到电子邮件或任何whatsapp、微信消息时,它会进入哪个方法。但是,我没有成功。
当我开始这个项目时,我在StartService(i)行上遇到了异常。所以请您检查一下我的代码,并告诉我我犯了什么错误。您的评论和建议对我和AccessibilityService初学者将非常有帮助。
我使用的代码如下:
在MainActivity中...
在AccessibilityService类中...
在 Manifest 文件中...
在res/xml文件夹中,即accessibilityservice.xml文件中...
当我开始这个项目时,我在StartService(i)行上遇到了异常。所以请您检查一下我的代码,并告诉我我犯了什么错误。您的评论和建议对我和AccessibilityService初学者将非常有帮助。
我使用的代码如下:
在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);
}
}
在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();
}
}
在 Manifest 文件中...
<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" />
我获取到的日志信息是...
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