I wrote a simple android app in Android Studio. It shows a button that starts a timer that plays a sound every X minutes until stopped with another button.
I use the following code that ran fine up to API33 – but if I test is on API34 or API35 it will finish the mButtonStart routine and the the app will crash.
Can anyone put me on the right track?
mButtonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!timerRunning) {
mZeitSlider.setVisibility(View.INVISIBLE);
mButtonStart.setVisibility(View.INVISIBLE);
startTimer(zeitInMinuten[0]*60000);
vibrator.vibrate(100);
mtextViewRemark.setVisibility(View.VISIBLE);
mTextAnstelleButton.setVisibility(View.VISIBLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
});
private void startTimer(long reminderRate) {
timerRunning = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(new Intent(MainActivity.this, MyService.class));
}
else {
//startService(new Intent(MainActivity.this, MyService.class));
}
startAlert(reminderRate);
}
public void startAlert(long reminderRate) { //as alternative to service
Intent intent = new Intent(this, MyBroadcastReceiver.class);
if (Build.VERSION.SDK_INT >= 23) {
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), myRequestCode, intent, PendingIntent.FLAG_IMMUTABLE);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
try {
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 3000, reminderRate, pendingIntent);
} catch(Exception e) { e.printStackTrace(); }
}
}