android - Call Function from other class -


i'm making android app using bluetoothchat example.

i need separate de bluetooth config activity chat activity, so, i've got main activity buttons enable/discoverable bluetooth, , button go chat activity.

the functions same in example, have divided them in 2 activities.

the thing need call setupchat () function main activity, call function when bluetooth enabled. i've got function in chat activity because there other variables in activity depend of it.

so, how can call function main activity??

i've readed methods. if function static call without problems, public void. cant neither put function in simple class because calls "findviewbyid" function , needs in activity.

so, how can call it?

i incorpore booth activities here:

this mainactivity:

public class btactivity extends activity {   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.main);      final button button1 = (button) findviewbyid(r.id.boton1);     final button button2 = (button) findviewbyid(r.id.boton2);     final button button4 = (button) findviewbyid(r.id.boton4);     final button button5 = (button) findviewbyid(r.id.boton5);      button5.setonclicklistener(new onclicklistener() {         @override         public void onclick(view view) {             lanzarcomunicacion (null);         }     });       mbluetoothadapter = bluetoothadapter.getdefaultadapter();     if (mbluetoothadapter == null) {         toast.maketext(this, "bluetooth not available", toast.length_long).show();         finish();         return;     }      button2.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             lanzarbusqueda(null);          }     });      button1.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             if (!mbluetoothadapter.isdiscovering()) {                  context context = getapplicationcontext();                 charsequence text = "making device discoverable";                 int duration = toast.length_short;                  toast toast = toast.maketext(context, text, duration);                 toast.show();                  intent discoverableintent = new intent(bluetoothadapter.action_request_discoverable);                 discoverableintent.putextra(bluetoothadapter.extra_discoverable_duration, 300);                  startactivity(discoverableintent);             }         }     });      button4.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             mbluetoothadapter.disable();              //out.append("turn_off bluetooth");             context context = getapplicationcontext();             charsequence text = "turning_off bluetooth";             int duration = toast.length_long;              toast toast = toast.maketext(context, text, 15);             toast.show();          }     }); }   @override public void onstart() {     super.onstart();      if (!mbluetoothadapter.isenabled()) {          intent enablebtintent = new intent(bluetoothadapter.action_request_enable);         startactivityforresult(enablebtintent, request_enable_bt);     }     else {         //here first place called         if (mtransmission == null) btcommunication.setupchat();     } }   @override public void onactivityresult(int requestcode, int resultcode, intent data) {     switch (requestcode) {         case request_connect_device:             // when devicelistactivity returns device connect             if (resultcode == activity.result_ok) {                 connectdevice(data);             }         case request_enable_bt:             // when request enable bluetooth returns             if (resultcode == activity.result_ok) {                 // bluetooth enabled, set chat session                   //here called                                 //btcommunication.setupchat();             } else {                 // user did not enable bluetooth or error occurred                 toast.maketext(this, r.string.bt_not_enabled_leaving, toast.length_short).show();                 finish();             }             break;     }  }   private void connectdevice(intent data) {     toast.maketext(getapplicationcontext(), "llega", toast.length_short).show();     // device mac address     string address = data.getextras().getstring(devicelistdialog.extra_device_address);     // bluetoothdevice object     bluetoothdevice device = mbluetoothadapter.getremotedevice(address);     // attempt connect device     mtransmission.connect(device);  }  @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.bt, menu);     return true; }   /** intent de llamada activity de comunicacion*/ public void lanzarcomunicacion (view view) {     intent = new intent(this, btcommunication.class);     startactivity(i); }  /** intent de llamada dialogo de busqueda de dispositivos*/ public void lanzarbusqueda (view view) {     intent serverintent = new intent(this, devicelistdialog.class);     startactivityforresult(serverintent, request_connect_device); } 

}

/ chat activity:

public class btcommunication extends activity {   @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.chat);      // performing check in onresume() covers case in bt     // not enabled during onstart(), paused enable it...     // onresume() called when action_request_enable activity returns.     if (mtransmission != null) {         // if state state_none, know haven't started         if (mtransmission.getstate() == transmission.state_none) {             // start bluetooth chat services             mtransmission.start();         }     } }    public void setupchat() {     // initialize array adapter conversation thread     mconversationarrayadapter = new arrayadapter<string>(this, r.layout.message);     mconversationview = (listview) findviewbyid(r.id.in);     mconversationview.setadapter(mconversationarrayadapter);      // initialize compose field listener return key     moutedittext = (edittext) findviewbyid(r.id.edit_text_out);     moutedittext.setoneditoractionlistener(mwritelistener);      // initialize send button listener click events     msendbutton = (button) findviewbyid(r.id.button_send);     msendbutton.setonclicklistener(new onclicklistener() {         @override         public void onclick(view v) {             // send message using content of edit text widget             textview view = (textview) findviewbyid(r.id.edit_text_out);             string message = view.gettext().tostring();             sendmessage(message);         }     });      // initialize transmission perform bluetooth connections     mtransmission = new transmission(this, mhandler);      // initialize buffer outgoing messages     moutstringbuffer = new stringbuffer(""); }    @override public void ondestroy() {     super.ondestroy();     // stop bluetooth chat services     if (mtransmission != null) mtransmission.stop(); }   /**  * sends message.  * @param message  string of text send.  */ public void sendmessage(string message) {     // check we're connected before trying     if (mtransmission.getstate() != transmission.state_connected) {         toast.maketext(this, r.string.not_connected, toast.length_short).show();         return;     }      // check there's send     if (message.length() > 0) {         // message bytes , tell bluetoothchatservice write         byte[] send = message.getbytes();         mtransmission.write(send);          // reset out string buffer 0 , clear edit text field         moutstringbuffer.setlength(0);         moutedittext.settext(moutstringbuffer);     } }  // action listener edittext widget, listen return key private final textview.oneditoractionlistener mwritelistener = new textview.oneditoractionlistener() {     @override     public boolean oneditoraction(textview view, int actionid, keyevent event) {         // if action key-up event on return key, send message         if (actionid == editorinfo.ime_null && event.getaction() == keyevent.action_up) {             string message = view.gettext().tostring();             sendmessage(message);         }         return true;     } };    private final void setstatus(int resid) {     final actionbar actionbar = getactionbar();     actionbar.setsubtitle(resid); }  private final void setstatus(charsequence subtitle) {     final actionbar actionbar = getactionbar();     actionbar.setsubtitle(subtitle); }    // handler gets information transmission private final handler mhandler = new handler() {     @override     public void handlemessage(message msg) {         switch (msg.what) {             case message_state_change:                 switch (msg.arg1) {                     case transmission.state_connected:                         setstatus(getstring(r.string.title_connected_to, mconnecteddevicename));                         mconversationarrayadapter.clear();                         break;                     case transmission.state_connecting:                         setstatus(r.string.title_connecting);                         break;                     case transmission.state_listen:                     case transmission.state_none:                         setstatus(r.string.title_not_connected);                         break;                 }                 break;             case message_write:                 byte[] writebuf = (byte[]) msg.obj;                 // construct string buffer                 string writemessage = new string(writebuf);                 mconversationarrayadapter.add("me:  " + writemessage);                 break;             case message_read:                 byte[] readbuf = (byte[]) msg.obj;                 // construct string valid bytes in buffer                 string readmessage = new string(readbuf, 0, msg.arg1);                 mconversationarrayadapter.add(mconnecteddevicename+":  " + readmessage);                 break;             case message_device_name:                 // save connected device's name                 mconnecteddevicename = msg.getdata().getstring(device_name);                 toast.maketext(getapplicationcontext(), "connected " + mconnecteddevicename, toast.length_short).show();                 break;             case message_toast:                 toast.maketext(getapplicationcontext(), msg.getdata().getstring(toast), toast.length_short).show();                 break;         }     } }; 

}

normally, have 2 ways handle this.

  1. mainacvitity offer public static instance

  2. use manager class hold instance of mainacvitity


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -