线程阻塞在Android的UI

编程入门 行业动态 更新时间:2024-10-28 12:21:14
本文介绍了线程阻塞在Android的UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用来自Android的蓝牙指南此示例code,发现这里的 developer.android/guide/topics/connectivity/bluetooth.html 作为连接客户端之下。

I am using this sample code from the Android Bluetooth guide, found here developer.android/guide/topics/connectivity/bluetooth.html under "Connecting as a Client".

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) { } }

}

在我的onCreate,我称之为

In my onCreate, I call

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

本的ConnectionManager启动一个ConnectThread和连接线成功地连接到另一台蓝牙设备。然而,布局不会呈现直到ConnectThread回报率(确切地说,当mmSocket.connect()停止阻塞)。我怎样才能获得的布局首先显示?

The connectionManager launches a ConnectThread and the connect thread successfully connects to another bluetooth device. However, the layout isn't rendered until the ConnectThread returns (exactly, when mmSocket.connect() stops blocking). How can I get the layout to display first?

推荐答案

记住,的setContentView()只需要一次行动的onCreate()方法返回。考虑到这一点,你应该运行在另一个线程,而不是在UI线程code。

Remember, setContentView() only takes action once the onCreate() method returns. Taking this into account, you should run the code in another thread and not in the UI thread.

您code应该是这个样子:

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(); }

更多推荐

线程阻塞在Android的UI

本文发布于:2023-06-07 22:46:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/572847.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   Android   UI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!