multithreading - How to access networking singleton object in separate thread in android? -


my singleton networking object:

public class communication implements runnable{      private static communication minstance = null;      private bufferedoutputstream socketoutput = null;     private printwriter printout = null;     private socket socket = null;     private scanner reader = null;               public static communication getinstance(){         if(minstance == null)         {             minstance = new communication();         }         return minstance;     }        public void login()     {  //login functionality          }     public void run()     {         //connect server using loopback address         try{             socket = new socket("192.168.0.3", 4242);              //set inputs , out puts             socketoutput = new bufferedoutputstream(socket.getoutputstream());             printout = new printwriter(socket.getoutputstream(), true);             reader = new scanner(socket.getinputstream());         }         catch( unknownhostexception uhe){             uhe.printstacktrace();         }         catch( ioexception ex){             ex.printstacktrace();         }     } } 

mainactivity want object instantiated

public class mainactivity extends actionbaractivity {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          new thread(communication.getinstance()).start();     } } 

that part works fine. instantiate communication object in new thread , connection established. want access object activity, unless getinstance() in new thread again networkonmainthread exception.

  1. does mean every time want use networking object have start new thread ?

  2. if (1) yes: how can access return value of communication.getinstance().login() main thread , put inside view object ?


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -