I have an app that does a small survey and as long as the app is open, it'll play a sound every 2 mins (to remind the user). But once the screen goes dark/app goes to back ground. It won't play the sound again.
How do I get it to repeat the sound, even when the app is in the background?. I tried calling the timer function in onStop()
but that didn't work
private MediaPlayer mp;
Timer timer;
TimerTask timerTask;
final Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mood);
addListenerOnNext();
mp = MediaPlayer.create(MoodActivity.this,R.raw.sound);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
nextbutton = (Button) findViewById(R.id.nextbutton);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if (radioGroup.getCheckedRadioButtonId() != -1)
{
nextbutton.setEnabled(true);
}
}
});
}
@Override
public void onStart()
{
super.onStart();
startTimer();
}
@Override
public void onStop(){
super.onStop();
}
@Override
protected void onResume(){
super.onResume();
startTimer();
}
public void startTimer(){
//set a new Timer
timer = new Timer();
//initialize the timer task
initializeTimerTask();
timer.schedule(timerTask,500,120000);
}
public void stopTimerTask(){
if(timer != null){
timer.cancel();
timer = null;
}
}
private void initializeTimerTask() {
timerTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
mp.start();
}
});
}
};
}