当我在API22上运行我的代码时,它可以正常工作,粘贴"测试测试"在我发布EditText
的应用中所需的AccessibilityEvent
。但是当我在API 17上运行它时,它不起作用。它将数据复制到剪辑但无法粘贴。我要求该机制适用于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();
}
}
答案 0 :(得分:8)
不幸的是,在API级别18中添加了AccessibilityNodeInfo.ACTION_PASTE,因此它无法使用API 17及更低版本。 AccessibilityNodeInfoCompat
只是现有功能的包装器,它不提供缺少功能的自定义实现。
v4支持库的来源非常清楚:
当您在performAction
中致电AccessibilityNodeInfoCompat
时,支持图书馆会调用IMPL.performAction
[1]
public boolean performAction(int action) {
return IMPL.performAction(mInfo, action);
}
当API级别为16和17时, IMPL
为AccessibilityNodeInfoJellybeanImpl
[2]
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();
}
这是performAction
[3]
AccessibilityNodeInfoJellybeanImpl
public static boolean performAction(Object info, int action, Bundle arguments) {
return ((AccessibilityNodeInfo) info).performAction(action, arguments);
}
正如您所见,支持库会调用标准performAction
的{{1}},因此,如果系统不支持android.view.accessibility.AccessibilityNodeInfo
,则v4支持库也不会支持ACTION_PASTE
。 t支持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"));
答案 1 :(得分:2)
也许你应该改进你的答案并添加一些关于你的进口的更多细节。
<强>粘贴:强> 正如您在评论中提到的,它更多的是再次从剪贴板中获取该东西?
从Android Copy and Paste Documentation 开始,你可以像这样的文字获取复制的内容:
// Examines the item on the clipboard. If getText() does not return null, the clip item contains the
// text. Assumes that this application can only handle one item at a time.
ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
// Gets the clipboard as text.
pasteData = item.getText();
// If the string contains data, then the paste operation is done
if (pasteData != null) {
return;
// The clipboard does not contain text. If it contains a URI, attempts to get data from it
} else {
Uri pasteUri = item.getUri();
// If the URI contains something, try to get text from it
if (pasteUri != null) {
// calls a routine to resolve the URI and get data from it. This routine is not
// presented here.
pasteData = resolveUri(Uri);
return;
} else {
// Something is wrong. The MIME type was plain text, but the clipboard does not contain either
// text or a Uri. Report an error.
Log.e("Clipboard contains an invalid data type");
return;
}
}
<强>复制:强>
ClipBoardManager
有两种不同的变体。 Honeycomb推出了新款产品。您必须确保您的代码使用了正确的变体。
看看这段代码示例:
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setText("text to clip");
} else {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
android.content.ClipData clip = android.content.ClipData.newPlainText("text label","text to clip");
clipboard.setPrimaryClip(clip);
}
这只是一个假设,因为这个问题错过了一些信息。