在服务中没有收到 CountDownTimer 回调

4
我有一个Bound Service,每15分钟收集3分钟的位置信息。我在ServiceConnectiononServiceConnected中启动一个CountDownTimer。在绑定了Service的Activity上,我完美地接收到所有计时器回调(onFinish) (onTick)。
当设备锁定时,我不会从计时器收到任何更新。

我的位置服务(MyLocationService)

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

我的活动

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();
            }
        };
    }

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

1个回答

0

当手机进入睡眠模式时,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);
 }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接