thread: to send data beetween class

Put your problem here if it does not fit any of the other categories.

thread: to send data beetween class

Postby gsources75 » Mon Feb 21, 2011 6:24 am

Hi, I know to load data from WebService o load other resource i need to creare a separate Thread. Ok i tryed and i't ok but all example show Thread inside the class but i'd like create a system of message between class, a second class load and parse data adn after sen data to firt class.

Sorry for mybad english, there is the code


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.example;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5.  
  6. public class MainClass extends Activity {
  7.     private UtilClass u_load;
  8.  
  9.         /** Called when the activity is first created. */
  10.     @Override
  11.     public void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.main);
  14.        
  15.         u_load= new UtilClass();
  16.         u_load.loadData("http://www.mysite.com/data.xml");
  17.        
  18.        
  19.     }
  20. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4






Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.example;
  2.  
  3. public class UtilClass {
  4.         public UtilClass() {
  5.                 // TODO Auto-generated constructor stub
  6.         }
  7.        
  8.         void loadData(String path){
  9.                
  10.                
  11.                 Thread t = new Thread() {
  12.             public void run() {
  13.                 mResults = doSomethingExpensive();
  14.                 mHandler.post(mUpdateResults);
  15.             }
  16.         };
  17.         t.start();
  18.         }
  19.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4

}


I would have the task of loading data is given to the second class and after send data to MainClass. I don't want create UtilClass as Activity
gsources75
Freshman
Freshman
 
Posts: 6
Joined: Sun Jan 16, 2011 6:07 pm

Top

Re: thread: to send data beetween class

Postby blundell » Thu Feb 24, 2011 11:23 pm

http://developer.android.com/reference/ ... ndler.html

Code: Select all
package com.example;

import android.app.Activity;
import android.os.Bundle;

public class MainClass extends Activity {
    private UtilClass u_load;
   
        /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        u_load= new UtilClass(mHandler);
        u_load.loadData("http://www.mysite.com/data.xml");       
    }

     private Handler mHandler = new Handler(){
             public handleMessage(Message msg){
                       switch(msg.what){
                        case 1:
                            // Data has finished loading
                            String s = u_load.getData();
                         break;
                       }
             }


     };
}


Code: Select all
package com.example;

public class UtilClass {
         
        private Handler mHandler; 
         
        public UtilClass(Handler handler) {
               mHandler = handler;
        }
       
        void loadData(String path){
               
               
                Thread t = new Thread() {
            public void run() {
                mResults = doSomethingExpensive();
                mHandler.sendEmptyMessage(1);
            }
        };
        t.start();
        }


        public void getData(){
          return mResults;

        }



Just a rough answer because i'm tired sorry :)
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 15 guests