通过ADB壳为API级别11集剪贴板文本 [英] Set clipboard text via adb shell as of API level 11

查看:426
本文介绍了通过ADB壳为API级别11集剪贴板文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在API级别11,它是可以通过使用服务程序上的亚行外壳

Before API level 11, it was possible to set the content of the clipboard by using the service program on the adb shell:

service call SERVICE CODE [i32 INT | s16 STR] ...
Options:
    i32: Write the integer INT into the send parcel.
    s16: Write the UTF-16 string STR into the send parcel.

有三个整数codeS定义方法:

There were three integer codes to define the methods:

1 TRANSACTION_getClipboardText
2 TRANSACTION_setClipboardText
3 TRANSACTION_hasClipboardText

例如此命令

$ adb shell service call clipboard 2 i32 1 i32 1 s16 "Hello Android!"

设置剪贴板的内容你好Android的!。由于API级别11所列出的方法是德precated 和新的取< A HREF =htt​​p://developer.android.com/reference/android/content/ClipData.html> ClipData 作为参数。如何通过现在设置剪贴板中的内容亚行外壳

set the clipboard's content to "Hello Android!". As of API level 11 the listed methods are deprecated and the new ones take ClipData as an argument. How do you set the clipboard content now via adb shell?

推荐答案

您已经问了两个不同的问题在这里。服务呼叫不相关的API函数。

You've asked two different questions here. The service calls are not related to the API functions.

Android是一般的过于积极地标记的API为德precated。在这种情况下,它只是意味着有更多的功能的新的功能。 的getText的功能() hasText()的setText()仍然存在,这些功能将继续工作,但他们现在实现为各地的新功能琐碎的包装。

Android is in general overly-aggressive about marking APIs as deprecated. In this case, it only means that there are new functions with more functionality. The functionality of getText(), hasText(), and setText() still exists and those functions will continue to work, but they are now implemented as trivial wrappers around the newer functions.

至于服务电话去了,那是一个内部的实现细节,因为你已经注意到不能保证整个的Andr​​oid版本一起使用。如果你窥探了Android <一href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.1.2_r1/android/content/IClipboard.java/">source code ,你会发现当前定义的这些交易:

As far as the service calls go, those are an internal implementation detail and as you've noticed are not guaranteed to work across Android versions. If you peer into the Android source code, you'll find these transactions currently defined:

TRANSACTION_setPrimaryClip = 1
TRANSACTION_getPrimaryClip = 2
TRANSACTION_getPrimaryClipDescription = 3
TRANSACTION_hasPrimaryClip = 4
TRANSACTION_addPrimaryClipChangedListener = 5
TRANSACTION_removePrimaryClipChangedListener = 6
TRANSACTION_hasClipboardText = 7

该人士$ ​​C $ C也表明什么参数,这些交易需要。不幸的是,TRANSACTION_setPrimaryClip需要一个 ClipData ,这是不是一个I32或S16,因此不符合服务电话兼容。我们比不过更大的问题;这些交易需要调用的包名作为参数,并剪贴板服务验证指定的包名调用的UID匹配。当使用亚行外壳,调用uid是要么UID_ROOT或UID_SHELL,两者都不拥有任何包装,所以没有办法通过,办理入住手续。简单地说,新的剪贴板服务不能使用这种方式。

The source code also indicates what parameters these transactions require. Unfortunately, TRANSACTION_setPrimaryClip requires a ClipData, which is not an i32 or an s16 and thus is not compatible with service call. We have bigger problems than that however; these transactions require the calling package name as a parameter, and the clipboard service validates that the specified package name matches the calling uid. When using the adb shell, the calling uid is either UID_ROOT or UID_SHELL, neither of which owns any packages, so there is no way to pass that check. Simply put, the new clipboard service cannot be used this way.

你能做些什么这一切?您可以创建自己的服务从命令行操作剪贴板并安装到您的设备。我不知道是否有什么办法可以延长服务电话,但您可以使用上午startservice 作为一个合适的替代者。如果你已经创建和安装定制的剪贴板的服务,那么你可以调用它为:

What can you do about all this? You can create your own service for manipulating the clipboard from the commandline and install it onto your device. I don't know if there's any way to extend service call, but you can use am startservice as a suitable replacement. If you've created and installed that custom clipboard service, then you could invoke it as:

am startservice -a MySetClipboard -e text "clipboard text"

在code来实现这种服务可以是这样的:

The code to implement this service could look like this:

public MyService extends Service {
    public int onStartCommand(Intent intent, int flags, int startId) {
        String text = intent.getStringExtra("text");
        ClipboardManager.setText(text);
        stopSelf();
        return START_NOT_STICKY;
    }
}

服务应该有一个意图过滤器,声明它能够处理的 MySetClipboard 意图的行动。

这篇关于通过ADB壳为API级别11集剪贴板文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆