【问题标题】:Using Extended Class使用扩展类
【发布时间】:2014-04-26 22:52:46
【问题描述】:

我是 java / 面向对象语言的新手,想获得一些语法方面的帮助。

我在 ConnectThread.java 中定义了一个类

public class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();


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
        UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
        tmp = device.createRfcommSocketToServiceRecord(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) { }
}
}

从这里我尝试创建一个线程并通过在我的连接方法中编写此代码来连接该线程:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice targetdevice;
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) 
{
// Loop through paired devices
    for (BluetoothDevice device : pairedDevices) 
    {
         if (device.getName().equals("HC-06"))
            targetdevice = device;
    }
}
Thread writeThread = new Thread();
writeThread.ConnectThread(targetdevice);

我在最后一行得到错误,它说“方法 ConnectThread(BluetoothDevice) 未定义线程类型” 我想既然 ConnectThread 是 Thread 的扩展类,我可以使用它下面的方法。不是这样吗?这样做的正确方法是什么? 谢谢!

【问题讨论】:

    标签: java android class extends


    【解决方案1】:

    将最后两个字符串更改为:

     Thread writeThread = new ConnectThread(targetdevice);
    

    当您需要启动 ConnectThread 时,请使用 start() 方法:

     writeThread.start(); //If you need start run() method of ConnectThread.
    

    【讨论】:

    • 哦,这行得通!那么我是否正确地假设在创建该对象时运行与类名具有相同名称的方法?
    • 还有writeThread.start()和writeThread.run()一样吗?
    • writeThread.start() 在新线程中启动 run() 方法。您可以在这里获取更多信息Class Thread
    • 与类名同名的方法称为构造函数。你可以在这里找到更多信息docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多