【发布时间】:2015-01-19 05:05:44
【问题描述】:
在我的 SSHsocket 类中(不扩展或实现任何东西)我实例化 HandlerThread:
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 片段实例的属性为空(膨胀的视图、侦听器......我称之为控制器等)。所以显然调用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.. 或通过在消息中传递 runnable)更好,但显然我错过了 HandlerThread 概念中的一些基本内容。我还认为这可能与 mainActivity=(MainActivity)msg.obj 完成的 mainActivity 的传递引用有关
但我没有看到活动状态正在改变(监控 MainActivity onStop() 方法)
@Override
protected void onStop(){
Log.e("MainActivity is in onStop state","");
super.onStop();
}
最终目标是将用户决策传递回工作线程,并根据响应继续进行。能给点建议吗?
【问题讨论】:
标签: android