0

I am trying to accept socket connections from clients. But in my AsyncTask i came across two situations where I'll need to update UI thread. the first case where as soon as socket is connected, it should change the current content view. And 2nd case where I'm receiving constant stream of objects which I'm updating on UI through publishProgress(). How do I go about doing so?

public class MainActivity extends AppCompatActivity {
   myCustomDrawingView view;
   ....
   ....
   .

   public static MainActivity getContext(){return context;}
   ....
   ..
   public class myCustomDrawingView extends View {
   ..
   ..
   }
   setNewContentView(){
     view = new myCustomDrawingView(this);
     setContentView(view);
   }
}




class ServerTask extends AsyncTask<Void, Action, Void>
{
    private WeakReference<MainActivity> mainActivity;
    private Action action;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mainActivity = new WeakReference<>(MainActivity.getContext());
    }

    @Override
    protected Void doInBackground(Void... voids) {
        ObjectInputStream stream;

        try{
            mainActivity.get().server = new ServerSocket(3107);
            mainActivity.get().socket = mainActivity.get().server.accept();
            stream = new ObjectInputStream(mainActivity.get().socket.getInputStream());

            (mainActivity.get()).setNewContentView(); 
            /*I know this won't work....
              But I want something like this to happen. 
              What are Alternatives?*/

            while(true)
            {
                if(isCancelled()){
                    break;
                }
                action = (Action)stream.readObject();
                publishProgress(action);
            }
        }catch (Exception exception)
        {
            exception.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onProgressUpdate(Action... actions) {
        super.onProgressUpdate(actions);
        ((mainActivity.get()).myCustomDrawingView).draw(actions[0]);
    } 
}
InsaneCat
  • 2,115
  • 5
  • 21
  • 40
Nitish Prajapati
  • 139
  • 1
  • 11
  • Set a Callback .https://stackoverflow.com/questions/3398363/how-to-define-callbacks-in-android. – ADM Aug 30 '18 at 10:18
  • Seems interesting! I'll give it a try and let you know if it works. Thanks! – Nitish Prajapati Aug 30 '18 at 10:31
  • Well `ServerTask` does seems a Concrete class . If its an inner class then you can directly access Outer Class properties. – ADM Aug 30 '18 at 10:38
  • Are you sure that if I make it an inner class then it won't raise any issues. And I guess you're telling me this because of the fact that I used weak references and context, isn't it? – Nitish Prajapati Aug 30 '18 at 10:41
  • Also, even if I make it an inner class, it doesn't change the fact that I cannot touch UI thread from background task. This matter still remains the same – Nitish Prajapati Aug 30 '18 at 10:43
  • `ServerTask` is not public thats why said . Use a Concrete class as AsyncTask with a callback interface. And make it `LifeCycle` Aware . to update the view from non UI thread you still need a `Handler` or `runOnUiThread`. if its a single view then you can use `view.post()` too. – ADM Aug 30 '18 at 10:44

0 Answers0