Android 中未调用 onAccessibityEvent
onAccessibityEvent not called in Android
我试图获取一些有关辅助功能事件的教程。我找到了一个辅助功能教程。这很好,我在 eclipse 中实现了 & 运行 在我的平板电脑 5.1.1 中,nexus。
编码。
MyAccessibilityService.java
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();
String s=String.valueOf(event.getEventType());
Toast.makeText(MyAccessibilityService.this, s, Toast.LENGTH_LONG).show();
Toast.makeText(MyAccessibilityService.this, getEventText(event), Toast.LENGTH_LONG).show();
Log.v(TAG, String.format(
"onAccessibilityEvent: [type] %s [class] %s [package] %s [time] %s [text] %s",
getEventType(event), event.getClassName(), event.getPackageName(),
event.getEventTime(), getEventText(event)));
} `
private String getEventType(AccessibilityEvent event) {
switch (event.getEventType()) {
case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
return "TYPE_NOTIFICATION_STATE_CHANGED";
case AccessibilityEvent.TYPE_VIEW_CLICKED:
return "TYPE_VIEW_CLICKED";
case AccessibilityEvent.TYPE_VIEW_FOCUSED:
return "TYPE_VIEW_FOCUSED";
case AccessibilityEvent.TYPE_VIEW_LONG_CLICKED:
return "TYPE_VIEW_LONG_CLICKED";
case AccessibilityEvent.TYPE_VIEW_SELECTED:
return "TYPE_VIEW_SELECTED";
case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
return "TYPE_WINDOW_STATE_CHANGED";
case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED:
return "TYPE_VIEW_TEXT_CHANGED";
}
return "default";
}
private String getEventText(AccessibilityEvent event) {
StringBuilder sb = new StringBuilder();
for (CharSequence s : event.getText()) {
sb.append(s);
}
return sb.toString();
}
@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);
}
}
在此代码中,如果我删除 getEventType 和 getEventtext 函数,它会将事件类型显示为 64。如果我添加这两个功能,我将无法获得事件操作。我尝试了几次,然后打开我的应用程序以在辅助功能点击中收听事件。
我的清单文件如下
Androidmanifest.xml
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<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>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/accessibility" />
</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>
谁能帮我找出我在这段代码中的缺陷。提前致谢。
几天前刚在 Android 4.4 和 5.0 上试过,效果很好。
如果您已经在 config-xml("@xml/accessibility") 中定义了所有内容,则无需在 onServiceConnected() 中设置 AccessibilityServiceInfo。而在AndroidManifest.xml你在Application里设置了“android:permission”,不知道行不行
public class MyAccessibilitySvc extends AccessibilityService {
private static final String TAG = "MyAccessibilitySvc";
@Override
protected void onServiceConnected() {
super.onServiceConnected();
}
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
AccessibilityNodeInfo nodeInfo = event.getSource();
if (null == nodeInfo) return;
Log.i(TAG, "onAccessibilityEvent: event=" + event);
}
@Override
public void onInterrupt() {
}
}
在AndroidManifest.xml中:
<service android:name=".MyAccessibilitySvc"
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/accessibilitysvc_config"/>
</service>
xml 配置:
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeViewFocused|typeViewClicked"
android:accessibilityFeedbackType="feedbackAllMask"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true"
/>
我试图获取一些有关辅助功能事件的教程。我找到了一个辅助功能教程。这很好,我在 eclipse 中实现了 & 运行 在我的平板电脑 5.1.1 中,nexus。
编码。
MyAccessibilityService.java
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();
String s=String.valueOf(event.getEventType());
Toast.makeText(MyAccessibilityService.this, s, Toast.LENGTH_LONG).show();
Toast.makeText(MyAccessibilityService.this, getEventText(event), Toast.LENGTH_LONG).show();
Log.v(TAG, String.format(
"onAccessibilityEvent: [type] %s [class] %s [package] %s [time] %s [text] %s",
getEventType(event), event.getClassName(), event.getPackageName(),
event.getEventTime(), getEventText(event)));
} `
private String getEventType(AccessibilityEvent event) {
switch (event.getEventType()) {
case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
return "TYPE_NOTIFICATION_STATE_CHANGED";
case AccessibilityEvent.TYPE_VIEW_CLICKED:
return "TYPE_VIEW_CLICKED";
case AccessibilityEvent.TYPE_VIEW_FOCUSED:
return "TYPE_VIEW_FOCUSED";
case AccessibilityEvent.TYPE_VIEW_LONG_CLICKED:
return "TYPE_VIEW_LONG_CLICKED";
case AccessibilityEvent.TYPE_VIEW_SELECTED:
return "TYPE_VIEW_SELECTED";
case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
return "TYPE_WINDOW_STATE_CHANGED";
case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED:
return "TYPE_VIEW_TEXT_CHANGED";
}
return "default";
}
private String getEventText(AccessibilityEvent event) {
StringBuilder sb = new StringBuilder();
for (CharSequence s : event.getText()) {
sb.append(s);
}
return sb.toString();
}
@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);
}
}
在此代码中,如果我删除 getEventType 和 getEventtext 函数,它会将事件类型显示为 64。如果我添加这两个功能,我将无法获得事件操作。我尝试了几次,然后打开我的应用程序以在辅助功能点击中收听事件。
我的清单文件如下 Androidmanifest.xml
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<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>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/accessibility" />
</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>
谁能帮我找出我在这段代码中的缺陷。提前致谢。
几天前刚在 Android 4.4 和 5.0 上试过,效果很好。 如果您已经在 config-xml("@xml/accessibility") 中定义了所有内容,则无需在 onServiceConnected() 中设置 AccessibilityServiceInfo。而在AndroidManifest.xml你在Application里设置了“android:permission”,不知道行不行
public class MyAccessibilitySvc extends AccessibilityService {
private static final String TAG = "MyAccessibilitySvc";
@Override
protected void onServiceConnected() {
super.onServiceConnected();
}
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
AccessibilityNodeInfo nodeInfo = event.getSource();
if (null == nodeInfo) return;
Log.i(TAG, "onAccessibilityEvent: event=" + event);
}
@Override
public void onInterrupt() {
}
}
在AndroidManifest.xml中:
<service android:name=".MyAccessibilitySvc"
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/accessibilitysvc_config"/>
</service>
xml 配置:
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeViewFocused|typeViewClicked"
android:accessibilityFeedbackType="feedbackAllMask"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true"
/>