1

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();
                    }
                });
            }
        };

    }
cyberbemon
  • 3,000
  • 11
  • 37
  • 62
  • In a nutshell, you need to use a Service in Background in conjunction with a power lock. You can look up [WakefulBroadcastReceiver](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html). – Skynet Apr 13 '15 at 10:48
  • Use CountDownTimer thru work – cyberlobe Apr 13 '15 at 10:51
  • No timer will work once the screen goes dark - You need an explict handle to wake the CPU up and play your sound. Be very careful to release the lock once you are done or your app will keep draining the battery and users will not like that. Have a look at [this](http://stackoverflow.com/questions/21461191/alarmmanager-fires-alarms-at-wrong-time/21461246#21461246) – Skynet Apr 13 '15 at 10:52

2 Answers2

1

Check out this link. It describes how to keep screen awake, while your application is running.

I will suggest to use backgroung service which will run periodically. Check out this link

Run Service periodically

anurag_dake
  • 988
  • 13
  • 15
0

Where do i start?

You have 2 acceptable ways to continue performing actions while your application is not in the foreground (especially for periodic tasks); (started) Service and AlarmManager.

The started (rather than bount) Service has its own process which will remain alive even when you're app is not in the foreground which means you could use a Thread, Handler etc. (*NOT TimerTask) to perform repeating tasks no matter what happens.

The AlarmManager uses a system Service to start PendingIntents no matter if your application is alive or not.

As for TimerTask, there are many posts online stating why it is a poor choice for Android development, here's one example, please use IntentService, AsyncTask, Loaders, Handlers or almost anything else.

Royi Benyossef
  • 769
  • 5
  • 18
  • If I use a Handler, would it still work even if the app is in the background? or do I need to combine handler with Service/AlarmManager? – cyberbemon Apr 13 '15 at 11:38
  • @cyberbemon you still need Service or AlarmManager i just wanted to let you know TimerTask is wrong to begin with in Android. – Royi Benyossef Apr 13 '15 at 12:00