Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to read preferences upon BOOT_COMPLETED

Tags:

android

I am setting an alarm from my app, and when rebooting, I see I need to reset the alarm since it does not survive reboots. I created a broadcast receiver to receive BOOT_COMPLETED and this works so my manifest must be correct.

When I try this line of code below I get in trouble. I need to retrieve the time that I need to set the alarm to but it looks like I cannot access the prefs of my app (called S) because my app has never started. NullPointerException :

if ( S.prefs.getBoolean(S.SCHEDULEDSTATUS, false) == true ) { }

I suppose it should be obvious that I cannot read a public static final of an activity that has not been created.

Do I have to store my alarm time in a file or am I missing something here?

like image 260
mlw Avatar asked Nov 27 '25 00:11

mlw


1 Answers

You have to access it via the context you get in your reciever:

    public void onReceive(Context con, Intent intent) {

            final SharedPreferences settings = con.getSharedPreferences(PREFS, 0);

    boolean boolValue = settings.getBoolean(BOOL, false);
}
like image 189
ninjasense Avatar answered Nov 29 '25 15:11

ninjasense