运行socket时android无法关闭对话框

时间:2012-06-20 01:41:05

标签: android sockets rdp

我有一些问题。 Dialog.dismiss()不起作用。   我想输入ip,用户名,密码登录WinServer 2003。单击“提交”按钮时,无法关闭对话框。需要注意的是,我的Thread-socket能够从Server检索消息并将消息发送回Server。只有在Thread-socket出错时才能关闭Dialog。

 public void onCreate(Bundle savedInstanceState) {      
    super.onCreate(savedInstanceState);
    setContentView(R.layout.jiemian);

    netInit();
    JieMianActivity.jiemian = this;
    LayoutInflater factory = LayoutInflater.from(JieMianActivity.this);  

    View view = factory.inflate(R.layout.login, null);  

    dialog02 = new AlertDialog.Builder(JieMianActivity.this)  
         .setIcon(android.R.drawable.btn_star)  
         .setTitle("login")  
           .setView(view)  
           .setPositiveButton("submit", onclickButton)  
           .setNegativeButton("cancel",  onclickButton).create();  
    dialog02.show(); 
}

private OnClickListener onclickButton = new OnClickListener() {

    @Override  
    public void onClick(DialogInterface dialog, int which) {  
        Log.v("which", which+"");
        switch(which){
        case Dialog.BUTTON_POSITIVE:
            dialog.dismiss();//doesn't work , cann't close dialog.
            EditText ip = (EditText) findViewById(R.id.ip);

            EditText username = (EditText) findViewById(R.id.username);

            EditText password = (EditText) findViewById(R.id.password);

            new Connect(JieMianActivity.jiemian).run();//do some socket thing
            break;
        case Dialog.BUTTON_NEGATIVE:
            dialog.dismiss();
            JieMianActivity.jiemian.finish();
            break;
        }
    }
};

这是我的主题:

 class Connect extends Thread{
    private JieMianActivity jiemain;
    public Connect(JieMianActivity jiemian){
        this.jiemain = jiemian;
    }

    public void run(){
        //Process.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
        try {
            Display display = getWindowManager().getDefaultDisplay();
            InputStream is = getResources().getAssets().open(
                    connect2RDP.mapFile);

            sfv = (SurfaceView) findViewById(R.id.surfaceView);

            sfh = sfv.getHolder();
            sfh.addCallback(JieMianActivity.jiemian);

            if (conn.connect("192.168.10.134", "Adminstrator", "123",
                    display.getWidth(), display.getHeight(), 3389, is)) {
                Log.v("login", "success");  

                //dialog02.dismiss();
                Log.v("login", "ok");

                canvas = new MyCanvas();
                canvas.setRop(new RasterOp());
                canvas.setHeight(Options.height);
                canvas.setWidth(Options.width);
                canvas.setRight(Options.width - 1);
                canvas.setBottom(Options.height - 1);
                canvas.setBackstore(new WrappedImage(Options.width,
                        Options.height, JieMianActivity.jiemian));
                canvas.setJiemian(JieMianActivity.jiemian);
                canvas.setSurView(sfv);
                canvas.setSurHolder(sfh);
                conn.doconnect(JieMianActivity.jiemian);// 启动RDP

                // init();
            }
        } catch (OrderException e) {

        } catch (Exception e) {

        }
    }
};

2 个答案:

答案 0 :(得分:0)

您需要更改代码以启动线程,您需要调用方法start() - 它将执行在该线程中编写的run()方法。
所以, 在Connect.start()处理程序中调用Connect.run()而不是onClick

答案 1 :(得分:0)

我认为您应该在UI线程中关闭警报对话框,否则它将无法正常工作。您可以通过两种方式执行此操作:1。使用消息处理程序2.使用RunOnUiThread。样品供您参考:

1

messageHandler.sendEmptyMessageDelayed(unique_id, 500);

private Handler messageHandler = new Handler()
{
    @Override
    public void handleMessage(Message msg) {
        switch(msg.what) {

            case unique_id:
                // do here
                break;
        }
    }
};

2

Runnable hide_ui = new Runnable() {
   @Override
   public void run() {
     // do here                           
   }
 };
 runOnUiThread(hide_ui);