为什么当我锁定手机时,位置服务停止发送位置更新



我创建了后台服务来定期获取用户位置并将其发送到服务器,如下所示:'

public class SyncDataToServerService extends Service {
private static final String TAG = "SyncDataToServerService";
private ServerCall serverCall;
private SharedPreferences tempPref2;
private Timer timer;
LocationManager locationManager;
UtilityClass utilityClass;
PowerManager.WakeLock wakeLock;
PowerManager powerManager;
@Override
public void onCreate() {
Log.e(TAG, "onCreate");
serverCall = new ServerCall(this);
timer = new Timer();
utilityClass = new UtilityClass(this);
tempPref2 = getSharedPreferences(getString(R.string.temp_pref_name_2), Context.MODE_PRIVATE);
}
@SuppressLint("MissingPermission")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Log.e(TAG, "onStartCommand");
powerManager = (PowerManager) getSystemService(POWER_SERVICE);
if (powerManager != null)
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyApp::MyWakelockTag");
wakeLock.acquire();
//initialize and start Location service
startLocation();
//initialize and start the TimerTask's job
startTimer();
return START_STICKY;
}
@SuppressLint("MissingPermission")
private void startLocation() {
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (locationManager != null)
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
10, new MyLocationListener());
if (locationManager != null)
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,
10, new MyLocationListener());
}

@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
if (locationManager != null)
locationManager.removeUpdates(new MyLocationListener());
super.onDestroy();
}
private void syncPath(Location mLocation) {
Log.w(TAG, "Location: " + mLocation + "  Latitude: " + mLocation.getLatitude() + " & Longitude: "
+ mLocation.getLongitude());
if (tempPref2.getString(getString(R.string.attendance_key), "").equalsIgnoreCase("present")) {
serverCall.sendEmpPath(mLocation);
}
}
private void startTimer() {
TimerTask timerTask = new TimerTask() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void run() {
if (utilityClass.isInternetConnected()) {
if (serverCall != null) {
if (serverCall.requestQ.size() > 0)
Log.e(TAG, "Request queue size ==" + serverCall.requestQ.size());
serverCall.syncData();
}
}
}
};
//schedule the timer, to wake up every 1 second
timer.schedule(timerTask, 1000, 1000);
}
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
Log.e(TAG, "onLocationChanged: " + location);
Intent intent1 = new Intent("com.example.mc_project");
intent1.putExtra("location", location);
sendBroadcast(intent1);
if (location.getAccuracy() < 25)
syncPath(location);
wakeLock.release();
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
}`

一切正常,但是当我锁定手机时,它会停止发送位置更新。 我读到这是因为系统睡眠,必须使用唤醒锁,但它也无法正常工作。当我解锁手机时,它应该开始发送位置更新,但这也不会发生。我的服务不会停止,它始终处于运行状态。请在我做错或遗漏的地方提供帮助。

您应该使用前台服务并将位置更新代码放入其中。 操作系统在电源关闭时随时停止后台服务

最新更新