java - 试图停止执行2秒钟

标签 java android multithreading

我有一个按钮,单击后将调用启动线程的方法。但是该线程有个停止点,很好。我希望先完成线程,然后再执行其余代码。

我尝试使用wait(),但无法正常工作。

public void configure(Button arg0,TextView arg1) throws InterruptedException{

    //calling OpenDeviceListener
    readText=arg1;
    button= arg0;

    //handler
    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            Log.d("Handler","running");
            if (actualNumBytes != 0x00) {
                Log.d("handler","if");
                readText.append(String.copyValueOf(readBuffer, 0,
                        actualNumBytes));
                Log.d("handler","if 312");
                actualNumBytes = 0;
                Log.d("handler","if end");
            }
            Log.d("handler","closed");
        }
    };


    Log.d("163","tab");
    uartInterface = new CH34xAndroidDriver(
            (UsbManager) getSystemService(Context.USB_SERVICE), this,
            ACTION_USB_PERMISSION);
    Log.d("167","tab");
    act_string = getIntent().getAction();
    if(-1 != act_string.indexOf("android.intent.action.MAIN"))
    {
        Log.d(TAG, "android.intent.action.MAIN 171");
    } 
    else if(-1 != act_string.indexOf("android.hardware.usb.action.USB_DEVICE_ATTACHED"))
    {
        Log.d(TAG, "android.hardware.usb.action.USB_DEVICE_ATTACHED 175");
    }

    if(!uartInterface.UsbFeatureSupported())
    {
        Toast.makeText(this, "No Support USB host API", Toast.LENGTH_SHORT).show();
        Log.d("182","tab");
        readText.setText("No Support USB host API");
        Log.d("184","tab");
        uartInterface = null;
    }

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

    if(READ_ENABLE == false) {
        READ_ENABLE = true;
        Log.d("192","tab");
        handlerThread = new readThread(handler);
        handlerThread.start();
    }
    Log.d("192","End");
    try{
    this.wait(100);
    }
    catch(InterruptedException e){
        e.printStackTrace();
        Log.d("Exception",e.getMessage());
    }
    // TODO Auto-generated method stub
                Log.d("205","OpenDeviceListener");
                boolean flags;
                if(false == isConfiged) {
                    Log.d("209","OpenDeviceListener");
                    Toast.makeText(global_context,""+((Boolean)uartInterface.isConnected()), Toast.LENGTH_LONG).show();
                    isConfiged = true;
            //      writeButton.setEnabled(true);
                    if(uartInterface.isConnected()) {
                        Toast.makeText(global_context,""+(Boolean)uartInterface.isConnected(), Toast.LENGTH_SHORT).show();
                        Log.d("213","OpenDeviceListener");
                        flags = uartInterface.UartInit();
                        if(!flags) {
                            Log.d(TAG, "Init Uart Error 2");
                            Toast.makeText(global_context, "Init Uart Error", Toast.LENGTH_SHORT).show();
                        } else {
                            if(uartInterface.SetConfig(baudRate, dataBit, stopBit, parity, flowControl)) {
                                Log.d(TAG, "Configed");
                            }
                        }
                    }

                }
}

和plz清除一件事,直到该线程将运行为止,都不会为该线程调用stop()。
谢谢

最佳答案

    final Runnable runnable = new Runnable() {

                        @Override
                        public void run() {
//this method will be called after 2 seconds
                            //do your task here
                        }
                    };
                    final Handler h = new Handler();
                    h.removeCallbacks(runnable); // cancel the running action (the
                                                    // hiding process)
                    h.postDelayed(runnable, 2000);

关于java - 试图停止执行2秒钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25986152/

相关文章:

java - Android应用程序和MySQL连接错误

java - 无法将 getString 用于 textView - android

java - 如何使用 OKHTTP 进行并发网络请求?

android - 在android中没有收到解析推送,它导致应用程序已停止弹出

C# + WinRT + 用于网络(Azure 移动服务)操作的 Monogame 线程

c++ - 多个线程在 C++ 中访问 std::map

java - 如何在从 cordova 创建的 native 代码中使用相同的 android SQLite DB?

java - Java 小程序的控制台 IO

java - 将字符串转换为 map 列表

java - 同步队列是否在每个报价/放置上创建一个线程?