gpt4 book ai didi

java - android 在某些情况下无法写入屏幕

转载 作者:行者123 更新时间:2023-12-01 14:58:56 24 4
gpt4 key购买 nike

我正在尝试使用 Android 中的库连接到终端模拟器,这将连接到串行设备并应显示发送/接收的数据。在这两种情况下,我应该能够通过终端下方的文本框或通过在终端本身中键入内容并按键盘上的 Enter 键来通过连接发送数据。库中有一个名为“write”的函数,用于写入模拟器屏幕。然而,有时这有效,有时则无效。

在我的代码中标记为 [1]、[2] 和 [3] 的行中,它工作正常,但对于 [4] 和 [5] 则不行。有人能看出为什么吗?我在 4 和 5 之前创建了终端 session ,所以它应该对他们有用,但事实并非如此。然而,当我开始为 1,2,3 调用 write 时,它​​工作正常吗?!

public class TermActivity extends Activity
{
private EditText mEntry;
private EmulatorView mEmulatorView;
private TermSession mSession;
private InputStream bis;
private OutputStream bos;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.term_activity);

/* Text entry box at the bottom of the activity. Note that you can
also send input (whether from a hardware device or soft keyboard)
directly to the EmulatorView. */
mEntry = (EditText) findViewById(R.id.term_entry);
mEntry.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int action, KeyEvent ev) {
// Ignore enter-key-up events
if (ev != null && ev.getAction() == KeyEvent.ACTION_UP) {
return false;
}
// Don't try to send something if we're not connected yet
TermSession session = mSession;
if (mSession == null) {
return true;
}

Editable e = (Editable) v.getText();
// Write to the terminal session
//for when i press enter on keyboard.
[1] session.write(e.toString());
[2] session.write("test");
[3] session.write('\r');
TextKeyListener.clear(e);
return true;
}
});



/**
* EmulatorView setup.
*/
EmulatorView view = (EmulatorView) findViewById(R.id.emulatorView);
mEmulatorView = view;

/* Let the EmulatorView know the screen's density. */
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
view.setDensity(metrics);

/* Create a TermSession. */
Intent myIntent = getIntent();
String sessionType = myIntent.getStringExtra("type");
TermSession session;

if (sessionType != null && sessionType.equals("telnet")) {
/* Telnet connection: we need to do the network connect on a
separate thread, so kick that off and wait for it to finish. */
// connectToTelnet(myIntent.getStringExtra("host"));

byte[] a = new byte[]{'y','y', 'y', 'y', 'y'};
byte[] b = new byte[]{'a','a', 'l', 'l', 'o'};
bis = new ByteArrayInputStream(b);
bos = new ByteArrayOutputStream();


session = new TelnetSession(bis, bos);


mEmulatorView.attachSession(session);
[4]session.write("test");
mSession = session;
[5]session.write("test");


return;
} else {
// Create a local shell session.
session = createLocalTermSession();
mSession = session;
}

/* Attach the TermSession to the EmulatorView. */
view.attachSession(session);

/* That's all you have to do! The EmulatorView will call the attached
TermSession's initializeEmulator() automatically, once it can
calculate the appropriate screen size for the terminal emulator. */
}

Socket mSocket;
private static final int MSG_CONNECTED = 1;

/* Create the TermSession which will handle the Telnet protocol and
terminal emulation. */
private void createTelnetSession() {
Socket socket = mSocket;

// Get the socket's input and output streams
InputStream termIn;
OutputStream termOut;
try {
termIn = socket.getInputStream();
termOut = socket.getOutputStream();
} catch (IOException e) {
//Handle exception here
return;
}

/* Create the TermSession and attach it to the view. See the
TelnetSession class for details. */
byte[] a = new byte[]{'y','y', 'y', 'y', 'y'};
byte[] b = new byte[]{'a','a', 'l', 'l', 'o'};
bis = new ByteArrayInputStream(b);
bos = new ByteArrayOutputStream();


TermSession session = new TelnetSession(bis, bos);
mEmulatorView.attachSession(session);
mSession = session;
session.write("test");
try {
bos.write(a);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

最佳答案

终端仿真器需要几秒钟来初始化连接并启动。在此之前,写入模拟器的字符串会被默默删除。

所以,你可以:

  • 如果不需要立即写入模拟器,请忽略此内容
  • 等待一段固定的时间
  • 检查终端模拟器 API 是否有查询连接状态的 API

关于java - android 在某些情况下无法写入屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13970813/

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