简体   繁体   English

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

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

I'm a beginner at Android Development and hoping someone can help me a bit out.我是 Android Development 的初学者,希望有人能帮助我。

I want to connect to a local server (IP).我想连接到本地服务器 (IP)。 I found a code in GitHub that supposedly would do this connection.我在 GitHub 中找到了一个代码,据说可以进行这种连接。 But the thing is that this is a java.class and not in my MainActivity .但问题是这是一个java.class而不是我的MainActivity So when I run my app in the emulator now, nothing happens.所以当我现在在模拟器中运行我的应用程序时,什么也没有发生。 How can I run the Java.class from inside my MainActivity ?如何从我的MainActivity中运行Java.class

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

Class: 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);
        }
    }
}

MainActivity:主要活动:


public class MainActivity extends AppCompatActivity {


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


}

Try this尝试这个

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


}

This should work if the code is correct.如果代码正确,这应该可以工作。 Think of the onCreate function of your Activities as the main function.将您的ActivitiesonCreate function 视为主要的 function。 You don't need another class for a task that small.对于这么小的任务,您不需要另一个 class。

Or else, you can otherwise also just create an instance of that class inside your onCreate method in the MainActivity and just call the main method that establishes the connection from that class with the object.否则,您也可以在MainActivityonCreate方法中创建该 class 的实例,然后调用从该 class 与 object 建立连接的main方法。

Either way, you'll be able to do it.无论哪种方式,您都可以做到。

You can learn more about the basics of Android Development , Activity , and Android Activity lifecycle so that you can understand this better.您可以了解更多关于Android DevelopmentActivityAndroid Activity 生命周期的基础知识,以便您更好地理解这一点。

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

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