0

i am working on a simple app. in my app i am using timerTask like this:

TimerTask saveData=new TimerTask() {
    @Override
    public void run() {
  saveDatatoFile();
    }
};

i call TimerTask in onCreate() of my activity like:

int file_saving_pollingtime=60000;
timer.schedule(saveData,10000,file_saving_pollingtime);

its working fine. i wants to change the file_saving_pollingtime on realtime basis for it i am using BroadcastReceiver to read message of specific pattern which contains the file_saving_pollingtime. i successfully read the file_saving_pollingtime from message and store it in sheared preference. but i am unable to refresh the file_saving_pollingtime of timer as per message file_saving_pollingtime:

if (frequency.matches(regexStr)){
                            editor.putString(FILE_FREQUENCY, frequency);
                            editor.commit();
                            int  fre=Integer.parseInt(sharedPreferences.getString(FILE_FREQUENCY, "0"));
                            int freq=fre*1000;
                            Log.d("dfdfdfdf", String.valueOf(freq));
                            Novipod mv=new Novipod(); //mv is the instance of main activity class
                            mv.timer.cancel();
                            mv.timer.schedule(mv.saveData,1000,freq);

                        }

please help me guys

1 Answers1

0

Try with following code:

public void rescheduleTimer() { // call it to reschedule the timer
    if(timer != null){
        timer.cancel();
    }
    timer = new Timer();
    timerTask = new MyTimerTask();
    timer.schedule(timerTask,1000,freq);
}

private class MyTimerTask extends TimerTask {
    @Override
    public void run() {
        saveDatatoFile();
    }
}
zeeali
  • 1,524
  • 20
  • 31