gpt4 book ai didi

android - AccessibilityService 中的粘贴在 API22 中有效,在 API17 中无效

转载 作者:IT老高 更新时间:2023-10-28 23:13:15 29 4
gpt4 key购买 nike

当我在 API22 上运行我的代码时,它工作得很好,将“测试测试”粘贴到启动 AccessibilityEvent 的应用程序中我所需的 EditText 中。但是当我在 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();
}
}

最佳答案

不幸的是AccessibilityNodeInfo.ACTION_PASTE在 API 级别 18 中添加,因此它不适用于 API 17 及更低版本。 AccessibilityNodeInfoCompat 只是现有功能的包装,它不提供缺失功能的自定义实现。

v4 支持库的来源非常清楚:

当您在 AccessibilityNodeInfoCompat 中调用 performAction 时,支持库调用 IMPL.performAction [1]

public boolean performAction(int action) {
return IMPL.performAction(mInfo, action);
}
当 API 级别为 16 和 17 时,

IMPLAccessibilityNodeInfoJellybeanImpl [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();
}

这是 AccessibilityNodeInfoJellybeanImpl 中的 performAction [3]

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"));

关于android - AccessibilityService 中的粘贴在 API22 中有效,在 API17 中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30020157/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com