Accessibility Service是Android系統(tǒng)框架提供給安裝在設(shè)備上應(yīng)用的一個(gè)可選的導(dǎo)航反饋特性。Accessibility Service 可以替代應(yīng)用與用戶交流反饋,比如將文本轉(zhuǎn)化為語音提示,或是用戶的手指懸停在屏幕上一個(gè)較重要的區(qū)域時(shí)的觸摸反饋等。本課程將教您如何創(chuàng)建一個(gè)Accessibility Service,同時(shí)處理來自應(yīng)用的信息,并將這些信息反饋給用戶。
@OverridepublicvoidonServiceConnected(){
// Set the type of events that this service wants to listen to. Others// won't be passed to this service.
info.eventTypes = AccessibilityEvent.TYPE_VIEW_CLICKED |
AccessibilityEvent.TYPE_VIEW_FOCUSED;
// If you only want this service to work with specific applications, set their// package names here. Otherwise, when the service is activated, it will listen// to events from all applications.
info.packageNames = new String[]
{"com.example.android.myFirstApp", "com.example.android.mySecondApp"};
// Set the type of feedback your service will provide.
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN;
// Default services are invoked only if no package-specific ones are present// for the type of AccessibilityEvent generated. This service *is*// application-specific, so the flag isn't necessary. If this was a// general-purpose service, it would be worth considering setting the// DEFAULT flag.// info.flags = AccessibilityServiceInfo.DEFAULT;
info.notificationTimeout = 100;
this.setServiceInfo(info);
}
@Override
publicvoidonAccessibilityEvent(AccessibilityEvent event) {
final int eventType = event.getEventType();
String eventText = null;
switch(eventType) {
case AccessibilityEvent.TYPE_VIEW_CLICKED:
eventText = "Focused: ";
break;
case AccessibilityEvent.TYPE_VIEW_FOCUSED:
eventText = "Focused: ";
break;
}
eventText = eventText + event.getContentDescription();
// Do something nifty with this text, like speak the composed string// back to the user.
speakToUser(eventText);
...
}
// Alternative onAccessibilityEvent, that uses AccessibilityNodeInfo
@Override
publicvoidonAccessibilityEvent(AccessibilityEvent event) {
AccessibilityNodeInfo source = event.getSource();
if (source == null) {
return;
}
// Grab the parent of the view that fired the event.
AccessibilityNodeInfo rowNode = getListItemNodeInfo(source);
if (rowNode == null) {
return;
}
// Using this parent, get references to both child nodes, the label and the checkbox.
AccessibilityNodeInfo labelNode = rowNode.getChild(0);
if (labelNode == null) {
rowNode.recycle();
return;
}
AccessibilityNodeInfo completeNode = rowNode.getChild(1);
if (completeNode == null) {
rowNode.recycle();
return;
}
// Determine what the task is and whether or not it's complete, based on// the text inside the label, and the state of the check-box.if (rowNode.getChildCount() < 2 || !rowNode.getChild(1).isCheckable()) {
rowNode.recycle();
return;
}
CharSequence taskLabel = labelNode.getText();
final boolean isComplete = completeNode.isChecked();
String completeStr = null;
if (isComplete) {
completeStr = getString(R.string.checked);
} else {
completeStr = getString(R.string.not_checked);
}
String reportStr = taskLabel + completeStr;
speakToUser(reportStr);
}
更多建議: