0

我有一个使用 CountDownTimer 的服务。

问题是 - 在onTick(...)函数内部CountDownTimer class,我需要使用

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

问题是我收到以下错误: FBBlockerService.FBServicelockCountdownTimer 类型的方法 getSystemService(String) 未定义

有没有办法解决这个问题?

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 - 
        }
    }


}
}
4

1 回答 1

2

采用

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

代替

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

因为this内部onTick方法是指FBServicelockCountdownTimer类上下文而不是FBBlockerService

于 2014-02-11T05:53:31.717 回答