2

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
        }        
    };
}
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
Ak-
  • 335
  • 4
  • 17
  • `OnCreate` is the first thing that runs, so unless you call the function within the `OnCreate` method before referencing the variable, it will have a value of 0.0 (default value). Maybe post some code so I can see exactly what is happening and what you need? – ArmaAK Jan 31 '14 at 18:53
  • 1
    Please post your code. Your explanation is a bit complicating. – FD_ Jan 31 '14 at 18:55
  • `and I even tried to write it in a "edit text" and parsing it in "on create()"` ...sounds like a logical problem... – FD_ Jan 31 '14 at 18:58
  • @FD_ ya seriously i read it three time but did not get anything. Ak- there is no need of defining public and static anything if you want to access in same class, there is something wrong in your logic . As suggested please include you code for the same. – Piyush Agarwal Jan 31 '14 at 19:00
  • thanks for reply guys I'm sorry about clarity in question :( I'm new to android and STACKOVERFLOW s.... – Ak- Jan 31 '14 at 19:11

4 Answers4

1

Simply initialize the variable as public static gobally in the class. You will be able to access it from anywhere.

fida1989
  • 3,234
  • 1
  • 27
  • 30
  • I tried to do so but even if it is GLOBAL (BAT variable) it gets back to BAT=0.0; even after calling disp(); which modifies the BAT value. – Ak- Jan 31 '14 at 19:15
  • You cannot have a global variable in an Activity. By definition, a global value has a life time and scope equal to that of the application. You are talking about a class field. – Simon Jan 31 '14 at 19:23
1

Define as public static your variables:

public class TryActivity extends Activity
{ 
public static  double BAT;  //global value.

public void onCreate(Bundle savedInstanceState) {
...
...
...

You are getting BAT with value of 0.0 because when your activity starts execute the method onCreate() and the the method disp() that only register the Intent to get the Battery Level.

If you want to get the battery level at the start of your activity you can do it with a function to get the battery level without receiving updates.

public float getMyBatteryLevel() {
        Intent batteryIntent = this.getApplicationContext().registerReceiver(null,
        new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        return batteryIntent.getIntExtra("level", -1);
}

@Override
protected void onCreate(Bundle savedInstanceState) {        
    //* Add this method.   
    getMyBatteryLevel()

    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 the right battery level.
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • I don't believe this is an issue of accessing the values from various classes, but rather various functions within the same class, namely the `OnCreate()` method and his user-defined function. – ArmaAK Jan 31 '14 at 19:10
  • I tried to make it static apparently when I access BAT value in "onCreate()" it gives 0.0 before displaying the BAT I called a function disp(); which modifies the BAT value (but BAT value is not changing outside disp(); ) and if we try to make it a member of a final class, in final we cannot modify the data members. So i can't set valueof BAT if it's final – Ak- Jan 31 '14 at 19:40
0

The problem is you don't understand the concept of concurrency. The BroadcastReceiver's onReceive() won't be called immediately. Thus, you are just setting up the BroadcastReceiver in disp(), and not directly touching BAT. BAT will only be filled with the correct value when onReceive() is called.

FD_
  • 12,947
  • 4
  • 35
  • 62
0

If you will look in to your logs the System.out.println() you have written in onCreate will be called before the System.out.println() written in onReceive of your BroadcastReceiver even it is written after your disp() method.

Reason :

In disp() method you are just registering your BroadcastRecever doesn't mean that your BroadcastReceiver is called. It will be called after sometime when your battery level will be changed.

Solution :

If you want to do something with yout BAT variable define a function in your Activity class and write whole logic inside it like

doThings(double batteryLevele){

    //write whatever you want to do with BAT

}

and call this function from onReceive method of your BroadcastReceiver.

Piyush Agarwal
  • 25,608
  • 8
  • 98
  • 111
  • so can't i able to access the BAT value in onCreate() ? PS: I tried the function "BATsetter(double level){BAT=level;}" in the BroadCastReciever(); but BAT value was still 0.0 in other function(not onCreate()). – Ak- Jan 31 '14 at 20:28
  • no, because onCreate will be called once and broacast wont be call that time, But y do want to access it in `onCreate()` only you can do whatever you want in a method and call it from your BroadcastReceiver . – Piyush Agarwal Feb 01 '14 at 03:47