I'm Trying to set a value(global) in a function and access it outside function that is in "On create()" in android I've tried making the global variable static, and I even tried to write it in a "edit text" and parsing it in "on create()" . but it keeps initializing to 0.0 (the variable is a double type) when i tried to access in "on create()",
oh and i can't return the value because the function is too nested so all hierarchy is too complex. :(
Can anyone help me with this;
public class TryActivity extends Activity
{
double BAT;\\ global value
public void onCreate(Bundle savedInstanceState)
{
disp(); // calling the function disp to set the value to BAT
String To_string=Double.toString(BAT);
System.out.println("Current Battery level ==="+To_string); \\ prints 0.0 the 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 the correct value
System.out.println("bbbattererrerey 1 "+Double.toString(BAT)); //prints the correct value
}
};
}