繁体   English   中英

如何从主要活动中运行 java.class

[英]How can I run a java.class from Main Activity

我是 Android Development 的初学者,希望有人能帮助我。

我想连接到本地服务器 (IP)。 我在 GitHub 中找到了一个代码,据说可以进行这种连接。 但问题是这是一个java.class而不是我的MainActivity 所以当我现在在模拟器中运行我的应用程序时,什么也没有发生。 如何从我的MainActivity中运行Java.class

这里是来源: https://github.com/calimero-project/introduction/tree/master/src/main/java

Class:

public class CreateTunnelingLink
{

    /**
     * Local endpoint, replace the IP address with your actual address. The local socket address is important for
     * multi-homed clients (several network interfaces), or if the address via InetAddress.getLocalHost is not useful.
     */
    private static final InetSocketAddress local = new InetSocketAddress("192.168.10.13", 3671);

    /**
     * Specifies the KNXnet/IP server to access the KNX network, insert your server's actual host name or IP address,
     * e.g., "192.168.1.20". The default port is where most servers listen on for new connection requests.
     */
    private static final InetSocketAddress server = new InetSocketAddress("myKnxServer.myHome",
            KNXnetIPConnection.DEFAULT_PORT);

    public static void main(final String[] args)
    {
        System.out.println("This example establishes a tunneling connection to the KNXnet/IP server " + server);

        // A KNX tunneling link supports NAT (Network Address Translation) if required.
        // We also indicate that the KNX installation uses twisted-pair (TP) medium, with TP1 being the most common.
        // KNXNetworkLink is the base interface implemented by all supported Calimero links to a KNX network.
        try (KNXNetworkLink knxLink = KNXNetworkLinkIP.newTunnelingLink(local, server, false, TPSettings.TP1)) {
            System.out.println("Connection established to server " + knxLink.getName());
            System.out.println("Close connection again");
        }
        catch (KNXException | InterruptedException e) {
            // KNXException: all Calimero-specific checked exceptions are subtypes of KNXException

            // InterruptedException: longer tasks that might block are interruptible, e.g., connection procedures. In
            // such case, an instance of InterruptedException is thrown.
            // If a task got interrupted, Calimero will clean up its internal state and resources accordingly.
            // Any deviation of such behavior, e.g., where not feasible, is documented in the Calimero API.

            System.out.println("Error creating KNXnet/IP tunneling link: " + e);
        }
    }
}

主要活动:


public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


}

尝试这个

public class MainActivity extends AppCompatActivity {

    private static final InetSocketAddress local = new InetSocketAddress("192.168.10.13", 3671);
    private static final InetSocketAddress server = new InetSocketAddress("myKnxServer.myHome",
            KNXnetIPConnection.DEFAULT_PORT);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // call your method to do the task
        yourMethodForMakingConnection();
    }

// for better separation you can create a method for this task
private void yourMethodForMakingConnection() {
        System.out.println("This example establishes a tunneling connection to the KNXnet/IP server " + server);

        // A KNX tunneling link supports NAT (Network Address Translation) if required.
        // We also indicate that the KNX installation uses twisted-pair (TP) medium, with TP1 being the most common.
        // KNXNetworkLink is the base interface implemented by all supported Calimero links to a KNX network.
        try (KNXNetworkLink knxLink = KNXNetworkLinkIP.newTunnelingLink(local, server, false, TPSettings.TP1)) {
            System.out.println("Connection established to server " + knxLink.getName());
            System.out.println("Close connection again");
        }
        catch (KNXException | InterruptedException e) {
            // KNXException: all Calimero-specific checked exceptions are subtypes of KNXException

            // InterruptedException: longer tasks that might block are interruptible, e.g., connection procedures. In
            // such case, an instance of InterruptedException is thrown.
            // If a task got interrupted, Calimero will clean up its internal state and resources accordingly.
            // Any deviation of such behavior, e.g., where not feasible, is documented in the Calimero API.

            System.out.println("Error creating KNXnet/IP tunneling link: " + e);
        }
}


}

如果代码正确,这应该可以工作。 将您的ActivitiesonCreate function 视为主要的 function。 对于这么小的任务,您不需要另一个 class。

否则,您也可以在MainActivityonCreate方法中创建该 class 的实例,然后调用从该 class 与 object 建立连接的main方法。

无论哪种方式,您都可以做到。

您可以了解更多关于Android DevelopmentActivityAndroid Activity 生命周期的基础知识,以便您更好地理解这一点。

暂无
暂无

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

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