【发布时间】:2022-01-19 15:12:27
【问题描述】:
我的应用有问题 它应该每 20-30 秒将当前位置发送到服务器,并且它曾经可以工作。 但是我们刚刚将它安装在使用 android 8.0 的新手机上,在这里我可以读到有一些背景位置限制,但我不知道如何让它们更快地获得一些更新。 atm 如果显示屏关闭,我每小时会收到 3-4 次更新
到目前为止,我正在像这样启动我的后台服务
intentService = new Intent(getBaseContext(), BackgroundService.class);
mLayout = findViewById(R.id.layout);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Log.e(TAG, "onCreate: Startet as foregroundservioce" );
startForegroundService(intentService);
} else {
Log.e(TAG, "onCreate: started as background service" );
startService(intentService);
}
后台服务onStartCommand()
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Let it continue running until it is stopped.
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
appPreferences = new AppPreferences(this.getBaseContext());
child = appPreferences.getChild();
lockStatic = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "taxaapp:WorkInBackground123");
lockStatic.acquire();
sslClientService = new SSLClientService(this);
return START_STICKY;
}
我的地理位置管理器
public class GeoLocationService {
private Timer mTimer;
private Alarm alarm;
private boolean tracking;
private LocationManager locationManager;
private BackgroundService _context;
private Child child;
private SSLClientService sslClientService;
private long alarmTimeToStop = 0;
private LocalDatabaseHandler ldbh;
private Handler mHandler = new Handler();
/**
* constructor initializes
*
* @param context
* @param child
*/
public GeoLocationService(BackgroundService context, Child child) {
_context = context;
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
sslClientService = _context.getSSLClient();
ldbh = new LocalDatabaseHandler(context);
mTimer = new Timer();
this.child = child;
}
/**
* Updates the working alarm
*
* @param alarm
*/
public void setAlarm(Alarm alarm) {
if (this.alarm.getId() == 0) {
this.alarm = alarm;
}
_context.sendMessageToUI(R.integer.MSG_GPS_START);
}
/**
* Returns the alarm being used at the moment
*/
public Alarm getAlarm() {
return alarm;
}
/**
* Starts location listener
*
* @return true if it was able to start
*/
public boolean start() {
boolean bool = false;
mTimer.schedule(new TimedTask(), 20, 30000);
if (!isTracking()) {
alarm = new Alarm(child.getId());
alarmTimeToStop = System.currentTimeMillis() / 1000L + _context.getResources().getInteger(R.integer.GpsMaxTime);
} else {
if (!alarm.getAlarmCoordinates().isEmpty()) {
alarm = new Alarm(child.getId());
alarmTimeToStop = System.currentTimeMillis() / 1000L + _context.getResources().getInteger(R.integer.GpsMaxTime);
// sslClientService.SendAlarm(new Alarm(child.getId(), alarm.getLastAlarmCoordinates().getAddress()));
}
}
// Register the listener with the Location Manager to receive location updates
if (ActivityCompat.checkSelfPermission(_context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(_context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(_context, Manifest.permission.ACCESS_BACKGROUND_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Log.e("Missing permissions", "we are missing some permissions for GPS location");
bool = false;
}
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, _context.getResources().getInteger(R.integer.GeoSettingsDistance), locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, _context.getResources().getInteger(R.integer.GeoSettingsDistance), locationListener);
locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, locationListener);
tracking = true;
bool = true;
ldbh.addLog("Starting new alarm", 200, "GeoLocationService-Start");
}
return bool;
}
/**
* is the tracking active
*/
/**
* Listens for location updates
*/
private LocationListener locationListener = new LocationListener() {
private long time = 0;
/**
* Called when the location has changed.
* <p> only accepts new location if 10seconds has passed and location accuracy is less than 30
* <p> There are no restrictions on the use of the supplied Location object.
*
* @param location The new location, as a Location object.
*/
@Override
public void onLocationChanged(Location location) {
Log.i(TAG, "onLocationChanged: Location Accuracy and provider: " + location.getAccuracy() + " / " + location.getProvider());
Log.i(TAG, "onLocationChanged: TIME! " + (location.getTime() - time));
if (location.getTime() - time > _context.getResources().getInteger(R.integer.GeoSettingsTime) && location.getAccuracy() <= _context.getResources().getInteger(R.integer.GpsAccuracy)) {
time = location.getTime();
// Called when a new location is found by the network location provider.
List<Address> address;
double lat = location.getLatitude();
double lng = location.getLongitude();
// lat = 10.27657397; //Kan bruges til test af dårlige kordinator
// lng = 56.21009964; //Kan bruges til test af dårlige kordinator
AlarmCoordinates alarmCoordinates = new AlarmCoordinates(lat, lng, alarm.getId());
Log.i(TAG, "onLocationChanged: new coordinates!");
NewAlarmCoordinates(alarmCoordinates);
}
}
【问题讨论】:
标签: android android-location android-background