0

I Would like to make global variables that can be accessed from a broad receiver class (repeating Alarm). To make global variables I used this class:

public class GVar extends Application {
private Integer Hour;
private Integer Minute;

public void setHour(Integer hour) {
    Hour = hour;
}

public void setMinute(Integer minute) {
    Minute = minute;
}

public Integer getHour() {
    return Hour;
}

public Integer getMinute() {
    return Minute;
}
}

Then my Alarm looks like this:

public class Alarm extends BroadcastReceiver {
private static Integer counter=0;

@Override
public void onReceive(Context context, Intent intent) {
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
    wl.acquire();

    // Put here YOUR code.
    //start activity
    Integer H=((GVar) this.getApplication()).getHour();

    Intent intentone = new Intent(context.getApplicationContext(), UssdCallActivity.class);
    intentone.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intentone);
    //startActivity()

    //Toast.makeText(context, "Alarm! ("+counter.toString()+")", Toast.LENGTH_SHORT).show(); // For example
    counter++;
    wl.release();

}

getApplication() method can't be resolved. The same method works when called from MainActivity class.

  • firstly did u register application class in manifest ? If it's ok, you can try context.getHour() – ersinyildiz Jan 11 '17 at 21:03
  • Application is not designed to save your app's data, you should consider create another class for saving data, eg: a class that can access SharedPreference, or just a Singleton class – WenChao Jan 11 '17 at 21:39
  • You can check theese questions [http://stackoverflow.com/questions/24060862/application-class-variables-gets-uninitialised-in-android](http://stackoverflow.com/questions/24060862/application-class-variables-gets-uninitialised-in-android) [http://stackoverflow.com/questions/21810240/how-to-create-a-global-variable-in-android](http://stackoverflow.com/questions/21810240/how-to-create-a-global-variable-in-android) – ersinyildiz Jan 12 '17 at 06:16
  • @ersinyildiz yes registered in the manifest: android:name="com.example.application.AnsarAlsunaDonations.GVar" context.getHour() doesn't work – Yaseen Ibrahim Jan 12 '17 at 19:25

0 Answers0