I have an AlarmManager in Activity 'PopShowAlarms'. This is a List of alarms, which the user can activate and deactivate. If he does so, the AlarmManager creates a new Alarm. Obviously the user also needs to be able to add alarms. He can do this in the second activity 'PopAddAlarm'. To make this newly created alarm, 'PopAddAlarm' calls a method of 'PopShowAlarms'. There the new data should be converted into an alarm instantly. This always leads to the error, that I can't use SystemServices before onCreate.
First I tried to call the method simply by making a new object of the class
Class class = new Class();
class.method();
This resulted in the error: can't call SystemServieces before onCreate
Now I've been told, that I have to use an Interface to do so, but I still get the same error. Because I'm new to interfaces, I think that's where the mistake might be. Here is my code:
Activity 'PopShowAlarms':
public class PopShowAlarms extends AppCompatActivity implements interfaceForwardAlarm {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pop_show_alarms);
...
}
@Override
public void onSwitchChange(int position, boolean switchOnOff) {
// when user activates the alarm, this code is triggered
if (switchOnOff) {
RecyclerViewElement currentElement = savedList.get(position);
String time = currentElement.getmAlarmTime();
// deleted methods that convert String 'time' to a calendar object
setAlarm(Calendar);
}
}
});
...
public void setAlarm(Calendar calendar){
AlarmManager alarmManager = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
int count = getCount();
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, count, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
...
// this is the implemented method from the interface
@Override
public void forwardAlarm(Context context, String alarmTime, String activeDays, String motivation, String music) {
setCalendar(alarmTime);
}
}
Second Activity: 'PopUpAdd
public class PopupAdd extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_popupadd);
...
imageViewAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int hour = numberPickerHour.getValue();
int minute = numberPickerMin.getValue();
String activeTime = Integer.toString(hour)+":"+Integer.toString(minute);
...
// the chosen alarm Time is forwarded to the activity 'Pop Show Alarms'
PopShowAlarms popShowAlarms = new PopShowAlarms();
popShowAlarms.forwardAlarm(context, activeTime, activeDays, "motivation", "music");
finish();
}
The Mentioned interface:
public interface interfaceForwardAlarm {
void forwardAlarm(Context context, String alarmTime, String activeDays,
String motivation, String music);
}
I'd be greatful for any help. I often struggle with this problem. (Calling methods from different activities) I hope this is at least the right approach