0

I am very beginner in Android, I am creating android application, communication with Siemens PLC its working fine but if i click button only the data shown in android , I want to run this code in service I don't know how to add code(below) in service

protected String doInBackground(String... strings) {
            try{

                client.SetConnectionType(S7.S7_BASIC);
                int res = client.ConnectTo("10.0.2.2",0,1);
                if(res == 0)
                {
                    byte[] data = new byte[4];
                    res = client.ReadArea(S7.S7AreaDB,1,0,2,data);
                    ret = "Values  "+S7.GetWordAt(data,0);
                }
                else {
                    ret = "Err:"+S7Client.ErrorText(res);
                }
                client.Disconnect();
            }
            catch (Exception e)
            {
                ret= "Exe"+e.toString();
                Thread.interrupted();
            }
            return "Executed";
        }

Above code is working fine but this code added to service I create one service

public class MyService extends Service {
S7Client client = new S7Client();
String ret = "";
@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    PlcReader task=new PlcReader();
    task.execute("");
    return START_NOT_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}


public class PlcReader extends android.os.AsyncTask<String,Void,String>{
    @Override
    protected String doInBackground(String... strings) {

        try{

            client.SetConnectionType(S7.S7_BASIC);
            int res = client.ConnectTo("10.0.2.2",0,1);
            if(res == 0)
            {
                byte[] data = new byte[4];
                client.ReadArea(S7.S7AreaDB,1,0,4,data);
                ret = "Values  "+S7.GetWordAt(data,0);
            }
            else {
                ret = "Err:"+S7Client.ErrorText(res);
            }
            client.Disconnect();
        }
        catch (Exception e)
        {
            ret= "Exe"+e.toString();
            Thread.interrupted();
        }
        return "Executed";
    }

    @Override
    protected void onPostExecute(String s) {
        Toast.makeText(getApplicationContext(),ret,Toast.LENGTH_LONG).show();
    }
}

I added code with service but shows connection problem

Annisha
  • 65
  • 9

2 Answers2

0

See this example:-

public class ExtractService extends Service {


@Override
public int onStartCommand(Intent intent, int flags, int startId) {

                AsyncTask task=new AsyncTask();
                task.execute();

    return START_NOT_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}


public class AsyncTask extends android.os.AsyncTask<Void,Void,Void>{
    @Override
    protected Void doInBackground(Void... voids) {

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);

    }
  }
}
Vipul Chauhan
  • 189
  • 4
  • 19
0

You can call your AsyncTask from the method onStartCommand in the Service:

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // Let it continue running until it is stopped.
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        new MyAsyncTask().execute("stringParameter")

        return START_STICKY;
    }

A couple of tips:

I see that you return a String value from your AsyncTask, just avoid using

String result = new MyAsyncTask().execute("stringParameter").get()

As it will block your main thread (unless you start your async task in a different thread). A more appropriate solution is to handle the result in the onPostExecute method in your AsyncTask (just override it)

Another thing to think about is that AsyncTask is deprecated in Android R, some good alternatives are the java.util.concurrent package and RxJava library

Two Horses
  • 1,401
  • 3
  • 14
  • 35