How can I run a java.class from Main Activity

54
May 24, 2021, at 3:20 PM

I'm a beginner at Android Development and hoping someone can help me a bit out.

I want to connect to a local server (IP). I found a code in GitHub that supposedly would do this connection. But the thing is that this is a java.class and not in my MainActivity. So when I run my app in the emulator now, nothing happens. How can I run the Java.class from inside my MainActivity?

Here is the source: 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);
        }
    }
}

MainActivity:


public class MainActivity extends AppCompatActivity {

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

}
Answer 1

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. You don't need another class for a task that small.

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.

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.

READ ALSO
Virtual Environment in Django

Virtual Environment in Django

When we create virtual environment is it good practice to keep, our virtual environment folder and our project folder inside same root folder? If i am not wrong then every project have its own viretual environment so how can we remmember which project...

61
why UNION ALL command in mysql doesn't give back any results?

why UNION ALL command in mysql doesn't give back any results?

I am trying to merge two queries into one, but UNION is not working for meHere is the code:

47
wp_enqueue_script was called incorrectly

wp_enqueue_script was called incorrectly

I am not a developer, but I have a theme that is giving me the error wp_enqueue_script was called incorrectlyScripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks

48