对话框在单独的线程中

时间:2011-07-20 09:49:28

标签: java android multithreading

我在线程方面相当新,所以这可能是一个非常糟糕的问题..(: 我有一个线程在一个单独的类中运行到我的主要活动这一切都运行良好,我能够使用handler.Post(new Runnable() {...从线程访问我的主要活动的方法,这一切都很好,直到我尝试调用一个需要用户输入的对话框。

这是对话框的方法:

public boolean displayConfirm(String message, String positiveButton, String negativeButton, final displayConfirmer onOnClick) {
    AlertDialog.Builder builder = new AlertDialog.Builder(ISyncCRMActivity.this);
    builder.setMessage(message);
    builder.setCancelable(false);
    builder.setPositiveButton(positiveButton, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) {
            onOnClick.onClick(true);
        }
    });
    builder.setNegativeButton(negativeButton, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) {
            onOnClick.onClick(false);
        }
    });
    AlertDialog dialog = builder.create();
    dialog.show();
    return bool;
}


private boolean bool;
public interface displayConfirmer {
        public void onClick(boolean result);
    }

正如您所看到的,您传递了一条消息,要显示对话框以及是,否按钮文本,然后返回用户单击是(true)或否(false)的任何位置,我使用interface作为onClick()。所有这些都允许我使用这段代码来执行它......

displayConfirm("Is this a dialog?", "Yes", "No", new ISyncCRMActivity.displayConfirmer() {
                public void onClick(boolean result) {
                    // TODO Auto-generated method stub
                    Toast.makeText(ISyncCRMActivity.this, result ? "Yes" : "No", Toast.LENGTH_LONG).show();
                }
            });

这将等到用户点击是或否,然后继续执行我正常需要的代码,但在另一个帖子中。

所以在我的单独帖子中,这就是我正在做的事情:

handler.post(new Runnable() {
                    public void run() {
                        iSyncCRMActivity.displayConfirm("Is this a dialog?", "Yes", "No", new ISyncCRMActivity.displayConfirmer() {
                            public void onClick(boolean result) {
                                if (result) dialog = true;
                                else dialog = false;
                            }
                        });
                    }
                });
             if (dialog) {
                 //clicked yes
             } else {
                //clicked no
             }

如您所见,我想调用UI线程上的对话框,然后在单独的线程上处理响应。

我怎么能做到这一点?这甚至可以实现吗?! 任何帮助将不胜感激(:谢谢!

编辑:

这是错误:

    07-20 11:04:35.782: ERROR/global(16201): Deprecated Thread methods are not supported.
07-20 11:04:35.782: ERROR/global(16201): java.lang.UnsupportedOperationException
07-20 11:04:35.782: ERROR/global(16201):     at java.lang.VMThread.stop(VMThread.java:85)
07-20 11:04:35.782: ERROR/global(16201):     at java.lang.Thread.stop(Thread.java:1379)
07-20 11:04:35.782: ERROR/global(16201):     at java.lang.Thread.stop(Thread.java:1344)
07-20 11:04:35.782: ERROR/global(16201):     at com.millennium.isynccrm.Classes.TcpClient.disconnect(TcpClient.java:55)
07-20 11:04:35.782: ERROR/global(16201):     at com.millennium.isynccrm.Classes.SyncClient.stopSync(SyncClient.java:73)
07-20 11:04:35.782: ERROR/global(16201):     at com.millennium.isynccrm.Classes.SyncClient.recieveMessage(SyncClient.java:236)
07-20 11:04:35.782: ERROR/global(16201):     at com.millennium.isynccrm.Classes.TcpClient$1.run(TcpClient.java:34)
07-20 11:04:35.782: ERROR/global(16201): [ 07-20 11:04:35.782 16201:0x3f54 F/dalvikvm ]
07-20 11:04:35.782: ERROR/global(16201): Exception!!! threadid=10: thread exiting with uncaught exception (group=0x4001d810)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201): FATAL EXCEPTION: Thread-12
07-20 11:04:35.812: ERROR/AndroidRuntime(16201): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
07-20 11:04:35.812: ERROR/AndroidRuntime(16201):     at android.view.ViewRoot.checkThread(ViewRoot.java:2806)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201):     at android.view.ViewRoot.requestLayout(ViewRoot.java:594)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201):     at android.view.View.requestLayout(View.java:8125)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201):     at android.view.View.requestLayout(View.java:8125)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201):     at android.view.View.requestLayout(View.java:8125)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201):     at android.view.View.requestLayout(View.java:8125)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201):     at android.view.View.requestLayout(View.java:8125)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201):     at android.widget.TextView.checkForRelayout(TextView.java:5378)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201):     at android.widget.TextView.setText(TextView.java:2688)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201):     at android.widget.TextView.setText(TextView.java:2556)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201):     at android.widget.TextView.setText(TextView.java:2531)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201):     at com.millennium.isynccrm.ISyncCRMActivity.updateCurentTask(ISyncCRMActivity.java:282)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201):     at com.millennium.isynccrm.Classes.SyncClient.stopSync(SyncClient.java:74)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201):     at com.millennium.isynccrm.Classes.SyncClient.recieveMessage(SyncClient.java:236)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201):     at com.millennium.isynccrm.Classes.TcpClient$1.run(TcpClient.java:34)

1 个答案:

答案 0 :(得分:0)

我相信您正在等待设置对话框布尔值,然后再继续下一行代码。有很多方法可以做到这一点,我建议你阅读Java并发教程here。下面是快速解决方案让事情有效,但我确定它是最好的,因为我不熟悉android开发。但是,我认为Android中对话框的主要观点是它们是异步的(为什么不在回调中放下你想要发生的事情,而不是让另一个线程等待?)。

一个新类,记录是否按下了是或否:

public class WaitingDialog implements Runnable {
        volatile boolean finished;
        volatile boolean dialog;
        String message;
        String button1;
        String button2;

    public WaitingThread(String message, String button1, String button2) {
        this.button1 = button1;
        this.button2 = button2;
        this.message = message;
        finished = false;
    }

    @Override
    public void run() {
        iSyncCRMActivity.displayConfirm(message, button1, button2, new ISyncCRMActivity.displayConfirmer() {
            public void onClick(boolean result) {
                if (result) dialog = true;
                else dialog = false;
                finished = true;
            }
        });
    }

    public boolean isFinished() {
        return finished;
    }

    public boolean getDialog {
        return dialog;
    }
}

使用这个新类的现有代码:

WaitingDialog waitingDialog = new WaitingDialog("Is this a dialog?", "Yes", "No");

handler.post(waitingDialog);
//Wait here for onclick to be called before you proceed
while(!waitingDialog.isFinished())

if (waitingDialog.getDialog()) {
    //clicked yes
} else {
    //clicked no
}