how to pass messages to handler from another class

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

how to pass messages to handler from another class

Postby migon » Fri Jul 02, 2010 12:41 pm

hi how i can pass messages to handler in Someactivity.class from class DownloadItInThread.class

im new to Java, maybe its basic stuff, but you know... :)

all tutorials are writen in one class most of time and real program with many functionalities cannot be made like this.
i dont want start new activity for this operation
migon
Freshman
Freshman
 
Posts: 3
Joined: Wed Jun 09, 2010 1:01 pm

Top

Re: how to pass messages to handler from another class

Postby blundell » Fri Jul 02, 2010 12:49 pm

When you create your second class, you can pass variables into its constructor. Is this what you mean? This is basic java OO
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: how to pass messages to handler from another class

Postby migon » Fri Jul 02, 2010 3:05 pm

i know
i do it already but still geting err



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.app.Activity;
  2.  
  3. import android.os.Bundle;
  4.  
  5. import android.os.Handler;
  6. import android.os.Message;
  7.  
  8. import android.view.MenuItem;
  9. import android.widget.TextView;
  10.  
  11. public class Act extends Activity {
  12.  
  13.         /** Called when the activity is first created. */
  14.     @Override
  15.     public void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.         setContentView(R.layout.main);
  18.  
  19.     }
  20.  
  21.  
  22.     public boolean onOptionsItemSelected(MenuItem item) {  
  23.         switch (item.getItemId()) {  
  24.                         case R.id.mnu_writeme:
  25.  
  26.                                 Down df = new Down(messageHandler, this);
  27.                                 df.tHandleMsg();
  28.                                 break;                   
  29.              
  30.         }  
  31.  
  32.         return true;
  33.     }    
  34.    
  35.         public Handler messageHandler = new Handler() {
  36.                
  37.                 public void handleMessage(Message msg) {
  38.                         super.handleMessage(msg);
  39.                         switch (msg.what) {
  40.                         case 5:
  41.                                 TextView t = (TextView) findViewById(R.id.xml_dump_text);
  42.                                 t.setText("text to show");
  43.                                 break;                         
  44.                         }
  45.                 }
  46.         };
  47.  
  48.        
  49. }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import android.content.Context;
  3. import android.os.Handler;
  4. import android.os.Message;
  5.  
  6.  
  7. public class Down {
  8.  
  9.         private static Handler  hDown;
  10.         private static Context  ctxDown;
  11.        
  12.         public Down (Handler ha, Context ctx) {
  13.                 hDown = ha;
  14.                 ctxDown = ctx;
  15.         }
  16.        
  17.         public void tHandleMsg () {
  18.         Message msg = Message.obtain();
  19.         msg.what = 5;
  20.         hDown.sendMessage(msg);
  21.         }
  22.        
  23.  
  24. }
  25.  
  26.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


getting error Uncaugth Handler exception
migon
Freshman
Freshman
 
Posts: 3
Joined: Wed Jun 09, 2010 1:01 pm

Re: how to pass messages to handler from another class

Postby migon » Fri Jul 02, 2010 3:52 pm

actually, if i copy this to blank project it works well
i have it wrong somewhere else,
damn :D

thx for help
migon
Freshman
Freshman
 
Posts: 3
Joined: Wed Jun 09, 2010 1:01 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 21 guests