如何在服务中调用定时器



我有一个countdown timer,当用户Logged In时开始。10分钟后it Logged Out User.

它工作正常,但如果用户停止应用程序,则定时器停止工作,并且用户永远是logged in

所以我把这整个东西放在一个服务中,这样它就会工作,不管用户是否停止应用程序。

问题是,在Service中添加Countdown Timer后,这已经停止工作。我已经在debug mode上检查了它,它没有进入service - onCreate方法。

这是我调用服务的方式。

 startService(new Intent(this,LogoutService.class));

我的服务类,调用CountdownTimer Class,首先这段代码是在我现在写startService()的地方写的

public class LogoutService extends Service {
    @Override
    public void onCreate() {
        UserLoggedInTimer logoutTimer= new UserLoggedInTimer(60000,1000);
        logoutTimer.setContext(getApplicationContext());
        logoutTimer.start();
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

当我在没有服务的情况下调用它时,倒计时计时器工作得很好,但这里它只是没有进入服务,我希望它会工作得很好,一旦我的应用程序成功启动上述服务

将其放入onStartCommand()中,并使其返回START_STICKY

参考

最新更新