Android receive and send message -


i have 1 activity , 1 ordinary class, activity 1 receive message , ordinary class send message. how implement it:

in activityone.class

@override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      public mhandler = new handler() {         public void handlemessage(message msg) {             super.handlemessage(msg);             switch (msg.what) {                 case 10:                    toast("get message 10");                    break;                 case 1:                    toast("get message 1");                    break;             }         }     }; }  public void toast(string text) {     toast.maketext(activityone.this, text, toast.length_short).show(); } 

and in ordinary.class

how code sendemptymessage(1) ?

you can use it

edit:

public class testapplication extends application{  private handler handler = null;        public void sethandler(handler handler) { this.handler = handler; }       public handler gethandler() { return handler; }   }  

then in activityone

protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     handler mhandler = new handler() {         public void handlemessage(message msg) {             super.handlemessage(msg);             switch (msg.what) {                 case 10:                    toast("get message 10");                    break;                 case 1:                    toast("get message 1");                    break;             }         }     };     testapplication mapp = (testapplication) getapplication();      mapp.sethandler(mhandler);   } 

activitytwo

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_activity_two);     testapplication mapp = (testapplication) getapplication();      mapp.gethandler().sendemptymessage(1);  } 

and must fix androidmanifest.xml add android:name=".testapplication"

<application     android:allowbackup="true"     android:name=".testapplication"     android:icon="@drawable/ic_launcher"     android:label="@string/app_name"     android:theme="@style/apptheme" > 

edit2:

you can use above code in ordinary classse or this:

public class ordinary{     private handler handler = null;        public ordinary(handler handler) { this.handler = handler; }       public void dosomething(){         handler .sendemptymessage(1);     } } 

in activityone:

ordinary clazz = new ordinary(mhandler); clazz.dosomething(); 

edit end


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -