Home > Mobile >  Background service cannot continue to get the problem, GPS positioning android9 and 10.
Background service cannot continue to get the problem, GPS positioning android9 and 10.

Time:05-16

Unbounded below there is a demand need continuous access to mobile phone positioning of GPS information, sends the information to the server at the same time,
Then adopted by starting the service running in the background, but found the background to monitor positioning can only get the first location data (LocationListener onLocationChanged methods), then there is no new data, determine not service died, because another message thread information from the server will still be able to receive,
The same code if it is running in the activity, there is no problem, can continue to get positioning,
Mobile phone android version is 9 and 10,
Checked many solutions were unable to online, the code is no problem for certain, it's been a lot of validation, in the service background GPS data is not available, and send the other data is no problem, now I wonder if the high version of the android in order to prevent malicious app, deliberately limit the availability of GPS positioning to backend service method, and must have interface to allow continuous access to GPS positioning,
Have the experience of students, if indeed there is the limit, then the demand will have to forget about it,

CodePudding user response:

The Service code is roughly the following
 
Package com. Example. LocationGPS;

The import android. App. Service;
The import android. Content. Intent;
The import android. The location. The location;
The import android. Location. LocationListener;
The import android. Location. LocationManager;
The import android. OS. Bundle;
The import android. OS. IBinder;
The import android. Support. The annotation. Nullable;

The import com. Example. Utils;
The import com. Sjzy. Androidlib. Util. HTTP. HttpUtil;

Public class LocationService extends the Service {
Private LocationUtil LocationUtil=null;

@ Override
Public void onCreate () {
Super. The onCreate ();
}

@ Nullable
@ Override
Public IBinder onBind (Intent Intent) {
return null;
}

@ Override
Public int onStartCommand (Intent Intent, int flags, int startId) {
/* *
* start GPS monitoring, it encapsulates the code, the actual use of the following code
* LocationManager LocationManager=(LocationManager) context. GetSystemService (context. LOCATION_SERVICE);
* locationManager. RequestLocationUpdates (locationManager. GPS_PROVIDER, 3000, 3, locationListener);
*/
LocationUtil=new locationUtil (LocationService. This, locationListener, LocationManager. GPS_PROVIDER);
LocationUtil. StartLocation ();

Return super. OnStartCommand (intent, flags, startId);
}

/* *
* location listener
*/
Private LocationListener LocationListener=new LocationListener () {
@ Override
Public void onLocationChanged Location (Location) {
Double latitude=location. GetLatitude ();
Double longitude=location. GetLongitude ();
String data=https://bbs.csdn.net/topics/latitude + "" + longitude;

//send data to the server
HttpUtil. Post (Utils. UploadLocation, String class, data, HttpUtil. DefaultEncode, Utils. GetAuthParam ());
}

@ Override
Public void onStatusChanged (String provider, int status, Bundle extras) {}
@ Override
Public void onProviderEnabled (String provider) {}
@ Override
Public void onProviderDisabled (String provider) {}
};
}


  • Related