首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从AccessibilityService粘贴工作在API22中在API17中不起作用

从AccessibilityService粘贴工作在API22中在API17中不起作用
EN

Stack Overflow用户
提问于 2015-05-04 06:31:41
回答 1查看 1.8K关注 0票数 18

当我在API22上运行我的代码时,它工作得很好,将“测试测试”粘贴到启动AccessibilityEvent的应用程序中所需的EditText中。但是当我在API 17上运行它时,它不工作。它将数据复制到clip,但无法粘贴。我需要在API 16和更高版本上工作的机制。

这是我到目前为止的代码:

代码语言:javascript
复制
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();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-07 17:40:24

不幸的是,AccessibilityNodeInfo.ACTION_PASTE是在API18级中添加的,因此它不能与API17及更低版本一起工作。AccessibilityNodeInfoCompat只是现有特性的包装器,它不提供缺少特性的自定义实现。

v4支持库的源代码非常清楚:

AccessibilityNodeInfoCompat中调用performAction时,支持库将调用IMPL.performAction [1]

代码语言:javascript
复制
public boolean performAction(int action) {
    return IMPL.performAction(mInfo, action);
}

当API级别为16和17 [2]时,IMPLAccessibilityNodeInfoJellybeanImpl

代码语言:javascript
复制
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

代码语言:javascript
复制
public static boolean performAction(Object info, int action, Bundle arguments) {
    return ((AccessibilityNodeInfo) info).performAction(action, arguments);
}

正如您所看到的,支持库调用标准android.view.accessibility.AccessibilityNodeInfoperformAction,所以如果系统不支持ACTION_PASTE,那么v4支持库也不支持ACTION_PASTE

您可以使用以下代码检查是否支持ACTION_PASTE

代码语言:javascript
复制
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"));
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30020157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档