简体   繁体   English

Android中的线程阻止UI

[英]Thread blocking UI in android

I am using this sample code from the Android Bluetooth guide, found here http://developer.android.com/guide/topics/connectivity/bluetooth.html under "Connecting as a Client". 我正在使用Android蓝牙指南中的示例代码,该示例代码位于http://developer.android.com/guide/topics/connectivity/bluetooth.html,位于“作为客户端连接”下。

private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;

public ConnectThread(BluetoothDevice device) {
    // Use a temporary object that is later assigned to mmSocket,
    // because mmSocket is final
    BluetoothSocket tmp = null;
    mmDevice = device;

    // Get a BluetoothSocket to connect with the given BluetoothDevice
    try {
        // MY_UUID is the app's UUID string, also used by the server code
        tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e) { }
    mmSocket = tmp;
}

public void run() {
    // Cancel discovery because it will slow down the connection
    mBluetoothAdapter.cancelDiscovery();

    try {
        // Connect the device through the socket. This will block
        // until it succeeds or throws an exception
        mmSocket.connect();
    } catch (IOException connectException) {
        // Unable to connect; close the socket and get out
        try {
            mmSocket.close();
        } catch (IOException closeException) { }
        return;
    }

    // Do work to manage the connection (in a separate thread)
    manageConnectedSocket(mmSocket);
}

/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
    try {
        mmSocket.close();
    } catch (IOException e) { }
}

} }

In my onCreate, I call 在我的onCreate中,我打电话

protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.activity_main);
    super.onCreate(savedInstanceState);
    connectionManager= new ConnectionManager(this);
    connectionManager.start();

The connectionManager launches a ConnectThread and the connect thread successfully connects to another bluetooth device. connectionManager启动一个ConnectThread,并且连接线程成功连接到另一个蓝牙设备。 However, the layout isn't rendered until the ConnectThread returns (exactly, when mmSocket.connect() stops blocking). 但是,直到ConnectThread返回(确切地说,当mmSocket.connect()停止阻塞时),布局才会呈现。 How can I get the layout to display first? 如何使布局首先显示?

In stead of creating the connection manager from the UI Thread, why dont you create it in a worker thread as follows: 代替从UI线程创建连接管理器,为什么不按如下所示在工作线程中创建连接管理器:

protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
new Thread(new Runnable() {

            @Override
            public void run() {
               connectionManager= new ConnectionManager(YourActivity.this);
               connectionManager.start();
            }
        }).start();

This way you never get to block the UI Thread, hope this helps... 这样您就永远不会阻塞UI线程,希望对您有所帮助...

Regards! 问候!

Remember, setContentView() only takes action once the onCreate() method returns. 请记住,只有onCreate()方法返回时, setContentView()onCreate() Taking this into account, you should run the code in another thread and not in the UI thread. 考虑到这一点,您应该在另一个线程中而不是在UI线程中运行代码。

Your code should look something like this: 您的代码应如下所示:

protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.activity_main);
    super.onCreate(savedInstanceState);
    new Thread(new Runnable() {

            @Override
            public void run() {
               connectionManager= new ConnectionManager(this);
               connectionManager.start();
            }
    }).start();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM