我的初始问题是,当在文本字段上按住点击时弹出“粘贴”气泡的单击操作。目前我还没有找到使用uiautomator脚本/代码来实现该操作的方法。因此,我开始考虑直接访问剪贴板。现在我遇到了在Android设备上访问剪贴板的问题。我们没有使用应用程序(APK),而是将JAR推送到设备上,然后使用adb runtest运行类。因此,没有启动任何活动。我猜这就是所有问题的根源。我创建了一个类文件,尝试访问剪贴板。但是,我目前得到了这个错误消息:“java.lang.IllegalStateException: System services not available to Activities before onCreate()”。我是Android和uiautomator的新手。我应该在哪里/如何添加onCreate()到这段代码中。我知道我们正在使用的过程最多也就是奇怪。任何帮助,无论是使用uiautomator点击“粘贴”气泡,还是让类正常工作,都将不胜感激。
目前我的类如下:
目前我的类如下:
import android.app.Activity;
import android.content.*;
import android.os.AsyncTask;
public class MyClipBoard extends Activity {
public String clip;
MyClipBoard() {
super.onCreate(null);
}
public void getClipBoard(){
new GetClipBoard().execute();
}
private class GetClipBoard extends AsyncTask<Void, Void, String> {
private String pMyClip;
@Override
protected String doInBackground(Void...params) {
try {
onCreate(null);
// ClipboardManager p = params[0];
String pasteData = "";
ClipboardManager myClipBoard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData.Item myClip = myClipBoard.getPrimaryClip().getItemAt(0);
CreateDeviceInfoFile.createInfoFile("Data from ClipBoard:", myClip.toString());
CreateDeviceInfoFile.createInfoFile("Number of Items:", String.valueOf(myClipBoard.getPrimaryClip().getItemCount()));
pMyClip = myClip.toString();
}catch (Exception e){
CreateDeviceInfoFile.createInfoFile("ERROR",e.toString());
}
// Gets the clipboard as text.
return pMyClip;
}
@Override
protected void onPostExecute(String result) {
clip = result;
CreateDeviceInfoFile.createInfoFile("Data from PostExecute:", result);
}
}
}
---------Edited added class-------------------
public class MiApp extends Application {
public MiClipBoard newBoard;
private static Context appContext;
MiApp(){
this.onCreate();
Looper.prepare();
newBoard = new MiClipBoard();
}
public MiClipBoard appClipBoard(){
return newBoard;
}
@Override
public void onCreate(){
super.onCreate();
}
public static Context getContext(){
return appContext.getApplicationContext();
}
}
public class MiClipBoard extends Activity {
private ClipboardManager clipboard;
MiClipBoard(){
Context context = MiApp.getContext();
clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
}
public void writeToClipBoard(String clipText){
try {
ClipData clip = ClipData.newPlainText("simple text", "Hello, World!");
clipboard.setPrimaryClip(clip);
CreateDeviceInfoFile.createInfoFile("Writing to ClipBoard", "Hello World");
} catch (Exception e){
CreateDeviceInfoFile.createInfoFile("Write Error", e.toString());
}
}
public void readClipBoard(){
String pasteData = "";
try {
ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
pasteData = item.getText().toString();
CreateDeviceInfoFile.createInfoFile("From ClipBoard", pasteData);
} catch (Exception e){
CreateDeviceInfoFile.createInfoFile("Read Error", e.toString());
}
}
}