android - 在服务中未收到 CountDownTimer 回调

标签 android service countdowntimer

我有一个 Bound 服务,每 15 分钟收集一次 3 分钟的位置信息。在 ServiceConnectiononServiceConnected 中连接服务后,我将启动一个 CountDownTimer。
我收到所有定时器回调 (onFinish) (onTick) 就 bindService 可见的 Activity 而言是完美的。
当设备被锁定时,我不会从计时器收到任何更新。

我的位置服务

public class MyLocationService extends Service implements MyTimerListener{

    private IBinder mBinder = new MyLocationBinder(); 

    public void onCreate() {
        locationManager = new MyLocationManager(this);
    }

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

    public class MyLocationBinder extends Binder
    {
        public MyLocationService getService()
        {
            return MyLocationService.this;
        }
    }

    public void startFetchingLocations()
    {
        if(locationManager != null){
            locationManager.startFetchingLocations();
            if(publishPeriodCountDownTimer != null) {
                publishPeriodCountDownTimer.cancel();
                publishPeriodCountDownTimer = null;
            }
            publishPeriodCountDownTimer = new MyCountTimer(gpsPublishPeriod * 60 * 1000, 1000, MyLocationService.this);
            publishPeriodCountDownTimer.start();    
        }
    }


    @Override
    public void onTimeFinished() {
        //Start fetching locations
        locationManager.startFetchingLocations(); //Start 3 min CountdownTimer
    }


    @Override
    public void onTick() {

    }

    public void onAllLocationsRecieved(ArrayList<Location> locations)
    {
        //Do stuff on Locations

        locationManager.stopFetchingGPS(); //Stops 3 min countdownTimer
    }
}

我的 Activity

public class MyActivity
    {

            @Override
            protected void onStart() {
                super.onStart();
                btnStop.setVisibility(View.VISIBLE);
                MyNoificationManager.cancelAllNotifications();

                if (!isLocationServiceBound) {
                    Intent locServiceIntent = new Intent(this, MyLocationService.class);    
                    bindService(locServiceIntent, locationServiceConnection, Context.BIND_AUTO_CREATE);
                }
        }

        private ServiceConnection locationServiceConnection = new ServiceConnection(){

            @Override
            public void onServiceDisconnected(ComponentName name) {
                isLocationServiceBound = false;

            }

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                MyLocationBinder locationBinder = (MyLocationBinder) service;
                locationService = locationBinder.getService();
                isLocationServiceBound = true;
                locationService.startFetchingLocations();
            }
        };
    }

当 Activity 可见时,它按预期工作。当设备锁定时,计时器不提供任何 onTick()onTimeFinished() 回调。
这可能是什么问题?

最佳答案

当手机进入休眠模式时,CountDownTimer 将不会工作。这是设计使然,旨在节省电池生命周期。

或者,您可以改用 android.app.AlarmManager 来实现您的目标。以下是如何执行此操作的示例代码。像下面这样设置闹钟

   AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
   Intent intent = new Intent(this, MyLocationService.class);
   intent.putExtra("need_to_fetch_loc", true);
   PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
   alarmManager.set(AlarmManager.RTC_WAKEUP, gpsPublishPeriod * 60 * 1000, alarmIntent);

将以下方法添加到您的 MyLocationService 类

 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {

     if(locationManager!= null && intent.getBooleanExtra("need_to_fetch_loc", false))
     {
         locationManager.startFetchingLocations();
     }

     return super.onStartCommand(intent, flags, startId);
 }

关于android - 在服务中未收到 CountDownTimer 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26925576/

相关文章:

python - TensorFlow 服务与 TensorFlow 服务

android - 如何在android中按后退时保持 Activity 状态不变?

android - 让 ImageView 保持文本的正确大小,同时防止长文本将其推出 View ?

Android:哪个更好 - 多个 Activity 或手动切换 View ?

java - OnSensorChanged线程

java - 问答游戏中的 CountDownTimer,NullPointer - Android (java)

javascript - 如何根据用户指定的时间填充一个100%的进度条

android - 我希望在关闭对话框 fragment 后刷新/重绘 Activity 。我该如何实现?

android - 出现键盘时向上滚动屏幕

javascript - 从 AngularJS 中的 Controller 调用服务函数