3

I am new to Android programming, I am trying to develop an app which is used to set alarms. As the AlarmManager will lose all the alarms on system reboot, so I saved all the alarms being created by the user using SharedPreferences.

I have a main activity, which lists all the alarms. I have an alarm_create activity which creates the alarms. alarm_receiver class is called as PendingIntent when the alarm goes on.

Now even if the alarm_receiver makes changes in the alarm, it is not reflected in the main_activity when the application resumes.

For saving the alarm, I have used the concept of queue which would help me retrieving the alarms in loop.

Please help me with the problem.

Sharda Singh
  • 727
  • 3
  • 10
  • 19
  • update your code that you have tried. – Tamilselvan Kalimuthu Mar 11 '13 at 08:17
  • Did you saved the alarm in the method onPause of all the activities that change the alarm? (see activity lifecycle: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle) – Laviniux Mar 11 '13 at 08:34
  • Show your code. Just explaining what you are trying to do without showing any code doesn't help anybody to help you. – Squonk Mar 11 '13 at 08:37
  • @Laviniux: I dint get you. – Sharda Singh Mar 11 '13 at 09:08
  • I have uploaded the code here.. https://code.google.com/p/alarm-control-panel/source/browse/#svn%2Ftrunk%2Fsrc%2Fcom%2Fexample%2Falarmcontrolpanel – Sharda Singh Mar 11 '13 at 09:20
  • You should save your data in onPause (or earlier) and load the saved data in onResume (onCreate is not enough). If I understand correctly, in your case you save the data at a certain option selected, but you don't reload it in onResume of AlarmCreator (I think this is why you don't see the changes from alarm_receviver). – Laviniux Mar 11 '13 at 10:51

3 Answers3

1

Im not quite sure I understand exactly what you want, but as I understand it you want to set the alarms again after the device has been rebooted.

1) Create a Reciever that listens for reboot:

public class BootReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        SharedPreferences settings = context.getSharedPreferences("YOUR SHAREDPREF NAME", 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("alarmName", "newAlarm");
        editor.commit(); // Don't forget to commit your changes!

   }
}

2) Add it in your androidmanifest.xml:

 <receiver android:name="com.yourpackage.BootReciever">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>

3) Add the permission:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
DecodeGnome
  • 1,809
  • 1
  • 19
  • 36
  • exactly I am looking for retrieving the saved SharedPrefences values without restarting the application. – Sharda Singh Mar 11 '13 at 09:06
  • Thnx for this, I was looking for this too – Sharda Singh Mar 11 '13 at 09:09
  • Updated my answer :) Remember to editor.commit(); and make sure to get the values (in your main_activity) from the the same SharedPreferences (using the same name). If this doesn't help you will have to post your code so we can figure out whats going on. – DecodeGnome Mar 11 '13 at 11:07
0

Below is the code snippet that we use to save the sharedPreferences

 Editor prefsEditor = preferences.edit();
prefsEditor.putString(CONFIG_INFO_KEY, configInfoJson);
prefsEditor.commit();

Hope this helps

Joe2013
  • 1,007
  • 1
  • 9
  • 24
  • Joe2013.. the way you are suggesting, I did the same thing but the problem I am facing is when the application returns to another activity after updating the value, that particular activity do not get the updated SharedPreferences values. :( – Sharda Singh Mar 11 '13 at 09:19
0

Actually, SharedPreferences do not get updated instantly, it gets updated when the Application is restarted.

Unfortunately, restarting an application is not that easy to code in Android, and is out of scope for your Application.

The above answer by DecodeGnome, can be used to relaod the list or reload the application's alarm.

I'll suggest you to go for SQLiteDatabase Class, and store the alarms in SQLite Database, as every Android device has SQLiteDatabase.

You can see the reference code here.

Veer Shrivastav
  • 5,434
  • 11
  • 53
  • 83