안드로이드의 라이브러리를 사용하여 터미널 에뮬레이터에 연결하려고합니다.이 장치는 직렬 장치에 연결되어 전송/수신 데이터를 표시해야합니다. 터미널 아래의 텍스트 상자를 통해 연결을 통해 데이터를 보내거나 터미널 자체를 입력하고 두 경우 모두 키보드에서 Enter 키를 눌러 데이터를 보낼 수 있어야합니다. 에뮬레이터 화면에 쓰려면 라이브러리에 '쓰기'라는 함수가 있습니다. 그러나 때로는 이것이 작동하고 그렇지 않은 경우도 있습니다.안드로이드가 화면에 쓸 수없는 경우가 있습니다.
내 코드에서 [1], [2] 및 [3]으로 표시된 행에서 [4] 및 [5]에 대해서는 제대로 작동합니다. 왜 아무도 볼 수 있습니까? 4와 5 이전에 터미널 세션을 만들었으므로 터미널 세션이 제대로 작동해야합니다. 그러나 1,2,3에 대한 쓰기를 시작하면 잘 작동합니까?!
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();
}
}
}
먼저, [4]와 [5] 행을 실행하고 있는지, 이중 긍정입니까? logcat이이를 확인합니다. 그럴 경우 다른 질문이 있습니다. 텔넷 세션이 초기화되고 터미널 서버에 연결하여 스레드를 설정하는 데 시간이 얼마나 걸립니까? 예를 들어 [4] 줄 5 초 전에 일시 중지했다면 작동합니까? –
그 덕분에, 나는 그것에 대해 몇 년 동안 기다려야했다. – Paul