【问题标题】:Not receiving CountDownTimer callbacks in Service未在 Service 中接收 CountDownTimer 回调
【发布时间】:2014-11-14 08:10:51
【问题描述】:

我有一个 Bound 服务,它每 15 分钟收集一次位置 3 分钟。一旦服务连接到onServiceConnectedServiceConnection,我就会启动一个CountDownTimer。
我收到所有的计时器回调 (onFinish) (onTick),就bindService 可见的活动而言。
当设备被锁定时,我不会从计时器收到任何更新。

我的位置服务

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

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

【问题讨论】:

    标签: android service countdowntimer


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-10
      • 1970-01-01
      • 2017-10-09
      • 2013-07-26
      • 2014-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多