android - Accessing the global value outside function -
i'm trying set value(global) in function , access outside function in "on create()" in android i've tried making global variable static, , tried write in "edit text" , parsing in "on create()" . keeps initializing 0.0 (the variable double type) when tried access in "on create()",
oh , can't return value because function nested hierarchy complex. :(
can me this;
public class tryactivity extends activity { double bat;\\ global value public void oncreate(bundle savedinstancestate) { disp(); // calling function disp set value bat string to_string=double.tostring(bat); system.out.println("current battery level ==="+to_string); \\ prints 0.0 wrong value super.oncreate(savedinstancestate); setcontentview(r.layout.main); } public void disp(){ this.registerreceiver(this.batteryinforeceiver, new intentfilter(intent.action_battery_changed)); } private broadcastreceiver batteryinforeceiver = new broadcastreceiver(){ public void onreceive(context context, intent intent){ double level= intent.getintextra(batterymanager.extra_level,0); bat=level; textview1 = (edittext) findviewbyid(r.id.textview1); textview1.settext(double.tostring(bat)); // sets correct value system.out.println("bbbattererrerey 1 "+double.tostring(bat)); //prints correct value } }; }
simply initialize variable public static gobally in class. able access anywhere.
Comments
Post a Comment