Tasker plugin android studio on recieve

Hello!

I'm trying to write a plugin for a tasker that will send sockets to the specified ip: port with a specific message.

Because in android and Java, I used a working project from github as an oak: https://github.com/nosybore/Tasker-MQTT-Publish-Plugin

I changed the fields variables and rewritten the code inside the handler itself:

public void onReceive(final Context context, final Intent intent) {
    mServer = intent.getStringExtra("Server");
    mPort = intent.getStringExtra("Port");
    mMessage = intent.getStringExtra("Payload");

    mMessage += "\r\n";

    try {
        //Send Socket
        if(mServer != "" && mPort != "" && mMessage != "") {
            Socket socket = new Socket(mServer, parseInt(mPort));
            DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
            dos.writeUTF(mMessage);
            dos.flush();
            dos.close();
            socket.close();
            Toast.makeText(context, "Socket Sended", Toast.LENGTH_SHORT).show();
        }
    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT).show();
        System.exit(1);
    }
}

In the tasker, the plugin sees the settings being added, but nothing happens at startup. I tried to output Toast - nothing is also output. Ch. i. d. n. t.? (

Author: Slava Vedenin, 2017-11-08

1 answers

And so everything turned out to be very difficult. Maybe someone will need the information.

1). To use java. sockets, you need to write in the manifest:

<uses-permission android:name="android.permission.INTERNET"/>

2). Android prohibits calling network functions from the main thread. So we create a new class with the constructor:

class SendSocket extends AsyncTask<Void, Void, Void> {
private String sMessage;
private int sPort;
private String sIpaddress;

public SendSocket(String msg, int prt, String ipaddress) {
    sMessage = msg;
    sPort = prt;
    sIpaddress = ipaddress;
}

@Override
protected Void doInBackground(Void... voids) {
    try {
        Socket socket = new Socket(sIpaddress, sPort);
        DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
        dos.writeUTF(sMessage);
        dos.flush();
        dos.close();
        socket.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}
}

And call it in onReceive:

SendSocket newSS = new SendSocket(mPayload, port, mServer);
newSS.execute();
 0
Author: Дмитрий, 2017-11-09 06:49:54