android - How to start a new intent / call a method in a handler? -
in case, have problem starting new intent or call method start intent if strreceived bluetooth matches final declared string str. how solve 'cannot make static reference non-static method getintent() type activity'.
i noticed cannot refer global strreceived value outside of handler:
public class standby extends listactivity { public final static string uuid = "00001101-0000-1000-8000-00805f9b34fb"; public final static string str = "\fdetect1\n\r"; public static string strread; public static string strreceived; bluetoothadapter bluetoothadapter; broadcastreceiver discoverdevicesreceiver; broadcastreceiver discoveryfinishedreceiver; //---store discovered devices--- arraylist<bluetoothdevice> discovereddevices; arraylist<string> discovereddevicesnames; public static textview txtdata; //---thread connecting client socket--- connecttoserverthread connecttoserverthread; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.standby); //---init arraylist objects , bluetooth adapter--- discovereddevices = new arraylist<bluetoothdevice>(); discovereddevicesnames = new arraylist<string>(); bluetoothadapter = bluetoothadapter.getdefaultadapter(); //---for displaying messages received--- txtdata = (textview) findviewbyid(r.id.txtdata); } //method make discoverable public void makediscoverable(view view) { intent = new intent( bluetoothadapter.action_request_discoverable); i.putextra( bluetoothadapter.extra_discoverable_duration, 300); //300 seconds startactivity(i); } //method discover other bluetooth devices private void discoveringdevices() { if (discoverdevicesreceiver == null) { discoverdevicesreceiver = new broadcastreceiver() { //fired when new device discovered @override public void onreceive(context context, intent intent) { string action = intent.getaction(); //when device discovered--- if (bluetoothdevice.action_found.equals(action)) { //---get bluetoothdevice object // intent--- bluetoothdevice device = intent.getparcelableextra( bluetoothdevice.extra_device); //---add name , address array // adapter show in listview--- //---only add if device not // in list--- if (!discovereddevices.contains(device)) { //---add device--- discovereddevices.add(device); //---add name of device; used // listview--- discovereddevicesnames.add(device.getname()); //---display items in listview--- setlistadapter(new arrayadapter<string>(getbasecontext(), android.r.layout.simple_list_item_1, discovereddevicesnames)); } } } }; } if (discoveryfinishedreceiver==null) { discoveryfinishedreceiver = new broadcastreceiver() { //fired when discovery done public void onreceive(context context, intent intent) { //---enable listview when discovery over; // 12 seconds--- getlistview().setenabled(true); toast.maketext(getbasecontext(), "discovery completed. please select bluebee.", toast.length_long).show(); unregisterreceiver(discoveryfinishedreceiver); } }; } //---register broadcast receivers--- intentfilter filter1 = new intentfilter(bluetoothdevice.action_found); intentfilter filter2 = new intentfilter(bluetoothadapter.action_discovery_finished); registerreceiver(discoverdevicesreceiver, filter1); registerreceiver(discoveryfinishedreceiver, filter2); //---disable listview when discover in progress--- getlistview().setenabled(false); toast.maketext(getbasecontext(), "discovery in progress...please wait...", toast.length_long).show(); bluetoothadapter.startdiscovery(); } //calls method discover other bluetooth devices public void discoverdevices(view view) { //---discover other devices--- discoveringdevices(); } static handler uiupdater = new handler(){ public void handlemessage(message msg) { int numofbytesreceived = msg.arg1; byte[] buffer = (byte[]) msg.obj; //---convert entire byte array string--- string strreceived = new string(buffer); //---extract actual string received--- strreceived = strreceived.substring( 0, numofbytesreceived); //---display text string str on textview in 1 line only--- //txtdata.settext(strreceived); //compare string. method return 0 if both identical if (strreceived.compareto(str)==0){ txtdata.settext("fall detected!!"); intent = new intent ("com.example.next"); //get data using bundle //add set of extended data intent , start bundle b = getintent().getextras(); i.putextras(b); startactivity(i); //readysms(); } } }; //fired when method called public void readysms() { // todo auto-generated method stub intent = new intent ("com.example.next"); //get data using bundle //add set of extended data intent , start bundle b = getintent().getextras(); i.putextras(b); startactivity(i); } public void onpause() { super.onpause(); //---cancel discovery of other bluetooth devices bluetoothadapter.canceldiscovery(); //---unregister broadcast receiver // discovering devices--- if (discoverdevicesreceiver != null) { try { unregisterreceiver(discoverdevicesreceiver); } catch(exception e) { } } //---if connected someone...--- if (connecttoserverthread!=null) { try { //---close connection--- connecttoserverthread.bluetoothsocket.close(); //calls method ready sms } catch (ioexception e) { log.d("mainactivity", e.getlocalizedmessage()); } } } //fired when client tapped in listview--- public void onlistitemclick(listview parent, view v, int position, long id) { //---if talking someone...--- if (connecttoserverthread!=null) { try { //---close connection first--- connecttoserverthread.bluetoothsocket.close(); } catch (ioexception e) { log.d("mainactivity", e.getlocalizedmessage()); //toast.maketext(this, "connect failed", toast.length_short).show(); } } //---connect selected bluetooth device--- bluetoothdevice deviceselected = discovereddevices.get(position); connecttoserverthread = new connecttoserverthread(deviceselected, bluetoothadapter); connecttoserverthread.start(); /* thread cancel = new thread(){ public void run(){ //this must same action name performed in manifest.xml file //if (txtdata.gettext().tostring().equalsignorecase(str)){ //if (strreceived!=null){ // strread = strreceived; // if (strread.compareto(str)==0){ try{ sleep(3000); intent = new intent ("com.nick.falldetection.cancel"); //get data using bundle //add set of extended data intent , start bundle b = getintent().getextras(); //string owner = b.getstring("abc"); //string message = b.getstring("longmessage"); //string phoneno = b.getstring("phoneno"); i.putextras(b); startactivity(i); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } finally{ //finish(); } } //} }; { cancel.start(); } */ }; }
you can solve having youractivity mactivity field in handler , setting this in activity's oncreate. way can reach youractivity has handler mhandler.mactivity.yourfieldormethod.
you this
class myactivity { static class myhandler extends handler() { myactivity mactivity; void handlemessage() { mactivity.somemethod(); } }; private myhandler uihandler = new myactivity(); private void somemethod() { /* ... */ } void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); uihandler.mactivity = this; } }
Comments
Post a Comment