裏面我SSHsocket類(不延長或實施任何東西)我實例HandlerThread:方法返回null,但對話框顯示
socketHandlerThread = new HandlerThread(sessionTag);
socketHandlerThread.start();
然後我調用connect()方法:
socketHandler = new Handler(socketHandlerThread.getLooper()) {
public void handleMessage(Message msg) {
switch (msg.what) {
case TerminalService.SERVICE_TO_SOCKET_DO_CONNECT:
try {
connect();
} catch (IOException e) {
Message statusMsg = Message.obtain(null,SOCKET_TO_SERVICE_STATUS_DEAD, sessionDetailData.getUuid());
serviceHandler.sendMessage(statusMsg);
Log.e("SSH Socket id:" + sessionDetailData.getUuid() + " fails. ", e.toString());
}
break;
裏面我需要打開一個肯定的connect()方法/無對話框:
final String titleMessage = "Do you want to accept the hostkey (type " + algo + ") from " + host + " ?\n";
mainActivity.runOnUiThread(new Runnable() {
public void run() {
FragmentTransaction fragmentTransaction=mainActivity.getFragmentManager().beginTransaction();
AcceptKeyDialog acceptKeyDialog = new AcceptKeyDialog();
acceptKeyDialog.show(fragmentTransaction, "KEY_ACCEPT_DIALOG");
acceptKeyDialog.getTitleView().setText(titleMessage);
}
});
會發生什麼情況,即使使用按鈕,對話框也會按預期填充。但是在調試時,runOnUiThread()中的(任何地方)斷點顯示acceptKeyDialog片段實例的屬性爲null(充氣視圖,監聽器......我稱之爲控制器等)。所以顯然調用AcceptKeyDialog的getTitleView()方法也會返回null。
public class AcceptKeyDialog extends DialogFragment {
private View keyDialogView;
//inner listener class for buttons
private AceeptKeyDialogFragmentController controller;
private TextView title;
private Button yesButton;
private Button noButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Window window = getDialog().getWindow();
window.setBackgroundDrawable(new ColorDrawable(Color.BLACK));
//DialogFragment.STYLE_NO_TITLE is not working as it should
window.requestFeature(Window.FEATURE_NO_TITLE);
controller = new AceeptKeyDialogFragmentController();
keyDialogView = inflater.inflate(R.layout.accept_key_dialog, container, false);
title = (TextView) keyDialogView.findViewById(R.id.accept_key_title);
yesButton = (Button) keyDialogView.findViewById(R.id.accept_key_yes_button);
noButton = (Button) keyDialogView.findViewById(R.id.accept_key_no_button);
title.setTextColor(Color.GREEN);
yesButton.setOnClickListener(controller);
noButton.setOnClickListener(controller);
return keyDialogView;
}
public TextView getTitleView(){
return title;
}
private class AceeptKeyDialogFragmentController implements View.OnClickListener {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.accept_key_yes_button:
break;
case R.id.accept_key_no_button:
break;
}
}
}
我想這可能是比使用處理器的信息(或handler.post ..或者通過消息傳遞中運行的),但很明顯,我錯過了一些在HandlerThread概念根本更好。我還以爲,這可能是一些涉及到mainActivity的通過參考其由mainActivity=(MainActivity)msg.obj
做,但我沒有看到活動狀況正在改變(監控MainActivity的onStop()方法)
@Override
protected void onStop(){
Log.e("MainActivity is in onStop state","");
super.onStop();
}
最後目標是將用戶決定傳遞迴工作線程,並根據響應繼續。你能建議嗎?