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.