2

I'm making an Alarm app which opens an activity based on time picked by User. Everything's working perfectly fine but alarm gets reset by reboot. I made an ON BOOT RECEIVER which works but I want to restart the Alarm only if the user wants it to go. I tried to use .putExtra to send data to ON BOOT broadcast but that data gets reset after reboot. I tried to set conditions using SharedPreference in broadcast but I'm stuck on how to use it. MainActivity

public class MainActivity extends AppCompatActivity {
NumberPicker hour;
NumberPicker min;
Button start;
Button stop;
TextView number;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    hour = (NumberPicker) findViewById(R.id.hour);
    min = (NumberPicker) findViewById(R.id.min);
    start = (Button) findViewById(R.id.starter);
    stop = (Button) findViewById(R.id.stopper);

    number = (TextView) findViewById(R.id.textView);

    hour.setMinValue(0);
    hour.setMaxValue(23);
    hour.setWrapSelectorWheel(false);

    min.setMinValue(1);
    min.setMaxValue(59);
    min.setWrapSelectorWheel(false);

    SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
    final int myIntValue = sp.getInt("your_int_key", -1);

    Intent myIntent = new Intent(MainActivity.this, BootCompletedReceiver.class);

    if (myIntValue == 1){

        myIntent.putExtra("alarm", 3);
        sendBroadcast(myIntent);

    }
    else if (myIntValue == 2){

       myIntent.putExtra("alarm", 4);
       sendBroadcast(myIntent);

    }
    else
    { stop.setBackgroundColor(Color.RED); }


    start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, hour.getValue());
            calendar.set(Calendar.MINUTE, min.getValue());

            Intent intent = new Intent(getApplicationContext(),notification.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 120*1000, pendingIntent);


            SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            editor.putInt("your_int_key", 1);
            editor.apply();

        }
    });

    stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getApplicationContext(),notification.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.cancel(pendingIntent);

            SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            editor.putInt("your_int_key", 2);
            editor.apply();

        }
    });

}}

Broadcast

public class BootCompletedReceiver extends BroadcastReceiver {



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

 if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {

        Intent i = new Intent(context, RestartAlarmsService.class);

        //ComponentName service = context.startService(i);
        context.startService(i);


    }
}}

Service

public class RestartAlarmsService extends Service {

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}
public int onStartCommand(Intent intent, int flags, int startId) {

    //code to restart Alarm

    return START_NOT_STICKY;
}

}

Thank you.

DroidGalaxy
  • 109
  • 11
  • Hello again @droidgalaxy What is your problem? You are not able to store info about alarms in shared preferences? – Marat Sep 12 '16 at 02:56
  • hey. I tried to do what you told me to do over the last days. couldn't find a way. I was thinking of using variables which change value upon alarm on and alarm off then pass that value to service and set if else statements to set alarm. It didnt work so Im not sure what to use to trigger only set alarms with time chosen by user – DroidGalaxy Sep 12 '16 at 03:02
  • You want to store only 1 alarm at a time or multiple alarms? – Marat Sep 12 '16 at 03:29
  • Multiple alarms . – DroidGalaxy Sep 12 '16 at 03:40
  • In this case, I would advice you to use SQLite database. It will be easier to implement functionality you want using SQLite. I could also give a hint on a code – Marat Sep 12 '16 at 03:44
  • do you know where i can go and learn how to use that? – DroidGalaxy Sep 12 '16 at 03:47
  • You can go here http://stackoverflow.com/a/38282237/6272369 and find a link on a video tutorial at the end of my answer. – Marat Sep 12 '16 at 03:52
  • I got the videos. Thanks. – DroidGalaxy Sep 12 '16 at 04:02
  • Not at all. If you will encounter any problems with designing your db, let me know. – Marat Sep 12 '16 at 04:05
  • @DroidGalaxy have you completed it for multiple alarms? – Rahul Dec 29 '18 at 08:12

0 Answers0