Question

I have a Service in which I am using a CountDownTimer.

Issue is that - inside the onTick(...) function of the CountDownTimer class, I need to use

ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);

Problem is that I get the following error: The method getSystemService(String) is undefined for the type FBBlockerService.FBServicelockCountdownTimer

Is there any way to get around this problem?

public class FBBlockerService extends Service {

int totalSeconds_Service;
boolean isRunning_Service;

Timer timer;

private CountDownTimer timerService;

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    timerService = new FBServicelockCountdownTimer(totalSeconds_Service * 1000, 1000);
    timerService.start();
    return START_NOT_STICKY; // need this START_REDELIVER_INTENT?
}

public class FBServicelockCountdownTimer extends CountDownTimer {

    public FBServicelockCountdownTimer(long millisInFuture,
            long countDownInterval) {
        super(millisInFuture, countDownInterval);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onFinish() {
        Log.v("fbblocker Service", "in onFinish FBServicelockCountdownTimer inside SERVICE");
        stopForeground(true);
    }

    @Override
    public void onTick(long millisUntilFinished) {
        ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
        // The first in the list of RunningTasks is always the foreground task.
        RunningTaskInfo foregroundTaskInfo = am.getRunningTasks(1).get(0);


                    //MORE OF MY CODE GOES HERE - 
        }
    }


}
}
Was it helpful?

Solution

use

ActivityManager am = (ActivityManager)FBBlockerService.this.
                                getSystemService(ACTIVITY_SERVICE);

instead of

ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);

because this inside onTick method refer to the FBServicelockCountdownTimer class context instead of FBBlockerService

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top