java - Send data from a Service's thread to an Activity -


I saw how to send data from the thread declared in this activity in this activity itself, however, I do not know the service How to get data from thread and then send it to my activity. I'm a bit confused, do I still need to use Handler?

Edit: I've got a solution! Let's do Yoga First of all, declare your handler within your activity and before starting your service, add some extra data by adding a new messenger linked to your handler.

  Public Class Home Activity ActionBarActivity extends {ConnectionDetector m_cd; Effect m_networkListenerService; Public handler m_handler = new handler () {@Override public void handleMessage // This is where the main activity thread receives the message {bundle bundle; If (msg.what == 1) // 1 Meaning: New Zealand {Bundle = msg.getData (); System.out.println (bundle .getString ("name")); } Super. HandleMessage (msg); }}; @ Override Protected Zero (Bundle Saved Instantstate) {Super. NET (Saved Instantstate); SetContentView (R.layout.activity_home); This.m_networkListenerService = New Intent (this, Network.class); This.m_networkListenerService.putExtra ("Messenger", New Messenger (this.m_handler)); This.startService (this.m_networkListenerService); After that, within your service class (you can do this with an IntentService class), get your Messenger back and send it as a parameter in your thread class:  
  public Provides class network service {NetworkListenerThread m_networkListenerThread; Messenger m_messenger; @ Override Public Empty on Create () {Super. Connet (); } @ Override Public Empty OnDesto () {Super. Onsteroy (); } @ Override public int start command (intent intent, int flag, int start id) {if (intention! = Null) {bundle extras = intent.get extras (;); If (additional! = Null) {this.m_messenger = (Messenger) additional .get ("Messenger"); This.m_networkListenerThread = New NetworkListenerThread (m_messenger); This.m_networkListenerThread.start (); }} Return Service. START_STICKY; }}  

Once again, get your Messenger back in your thread class:

  Public class NetworkListenerThread Thread Extended {Personal Stable FINAL INC. BROADCAST_PORT = 4415 ; Personal Datagram Socket m_socket = null; Private Messenger m_messenger; Public networkListread (Messenger Messenger) {this.m_messenger = messenger; }}  

Finally, wherever you want to send your data, use this code:

  message message = message. share (); // Message creates a message from the message handler to send the user back. Msg = 1; // Specifies the type of message bundle bundle = new bundle (1); // Object of Size 1 in which we put the data into the bundle. Input string ("name", name); // key + data (which is string) msg.setData (bundle); // Attach the bundle to the message {this.m_messenger.send (msg); } // We use messenger to send the data from the main thread (RemoteExpress E) {e.printStackTrace (); }  


Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -