当我在API22上运行我的代码时,它工作得很好,将“测试测试”粘贴到启动AccessibilityEvent
的应用程序中所需的EditText
中。但是当我在API 17上运行它时,它不工作。它将数据复制到clip,但无法粘贴。我需要在API 16和更高版本上工作的机制。
这是我到目前为止的代码:
public class MyAccessibilityService extends AccessibilityService {
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
AccessibilityNodeInfo nodeInf = null;
AccessibilityNodeInfo nodeInfo = null;
final int eventType = event.getEventType();
String eventText = null;
switch(eventType) {
case AccessibilityEvent.TYPE_VIEW_CLICKED:
eventText = "Clicked: ";
nodeInf = this.getRootInActiveWindow();
Log.d("AccessibilityNodeInfo", ""+ nodeInf.getChildCount());
nodeInf.recycle();
break;
case AccessibilityEvent.TYPE_VIEW_FOCUSED:
AccessibilityRecordCompat record = AccessibilityEventCompat.asRecord(event);
AccessibilityNodeInfoCompat source = record.getSource();
ClipboardManager clipboard = (ClipboardManager) this.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("label", "TESTING TESTING");
clipboard.setPrimaryClip(clip);
source.performAction(AccessibilityNodeInfoCompat.ACTION_PASTE);
//}
Log.d("AccessibilityNodeInfo", ""+ source.getClassName());
Intent intent = new Intent(MyAccessibilityService.this, TestActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
startActivity(intent);
break;
}
eventText = eventText + event.getText();
// Do something nifty with this text, like speak the composed string
// back to the user.
Log.d("Information", eventText);
Toast.makeText(getApplicationContext(), eventText + " " + android.os.Build.VERSION.SDK_INT,
Toast.LENGTH_LONG).show();
}
}
发布于 2015-05-07 17:40:24
不幸的是,AccessibilityNodeInfo.ACTION_PASTE是在API18级中添加的,因此它不能与API17及更低版本一起工作。AccessibilityNodeInfoCompat
只是现有特性的包装器,它不提供缺少特性的自定义实现。
v4支持库的源代码非常清楚:
在AccessibilityNodeInfoCompat
中调用performAction
时,支持库将调用IMPL.performAction
[1]
public boolean performAction(int action) {
return IMPL.performAction(mInfo, action);
}
当API级别为16和17 [2]时,IMPL
为AccessibilityNodeInfoJellybeanImpl
if (Build.VERSION.SDK_INT >= 22) {
IMPL = new AccessibilityNodeInfoApi22Impl();
} else if (Build.VERSION.SDK_INT >= 21) {
IMPL = new AccessibilityNodeInfoApi21Impl();
} else if (Build.VERSION.SDK_INT >= 19) { // KitKat
IMPL = new AccessibilityNodeInfoKitKatImpl();
} else if (Build.VERSION.SDK_INT >= 18) { // JellyBean MR2
IMPL = new AccessibilityNodeInfoJellybeanMr2Impl();
} else if (Build.VERSION.SDK_INT >= 16) { // JellyBean
IMPL = new AccessibilityNodeInfoJellybeanImpl();
} else if (Build.VERSION.SDK_INT >= 14) { // ICS
IMPL = new AccessibilityNodeInfoIcsImpl();
} else {
IMPL = new AccessibilityNodeInfoStubImpl();
}
这是AccessibilityNodeInfoJellybeanImpl
[3]中的performAction
public static boolean performAction(Object info, int action, Bundle arguments) {
return ((AccessibilityNodeInfo) info).performAction(action, arguments);
}
正如您所看到的,支持库调用标准android.view.accessibility.AccessibilityNodeInfo
的performAction
,所以如果系统不支持ACTION_PASTE
,那么v4支持库也不支持ACTION_PASTE
。
您可以使用以下代码检查是否支持ACTION_PASTE
:
AccessibilityNodeInfoCompat source = record.getSource();
int supportedActions = source.getActions();
boolean isSupported = (supportedActions & AccessibilityNodeInfoCompat.ACTION_PASTE) == AccessibilityNodeInfoCompat.ACTION_PASTE;
Log.d(TAG, String.format("AccessibilityNodeInfoCompat.ACTION_PASTE %1$s supported", isSupported ? "is" : "is NOT"));
https://stackoverflow.com/questions/30020157
复制相似问题