gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:10:01 24 4
gpt4 key购买 nike

我有一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com