在我的应用程序最小化或接收到呼叫或消息时,位置更新会在我的Oreo设备上正常工作,但在较旧的(棉花糖设备)上却没有记录位置,我在这里怎么做?
如我所见,调用onResume可以重新启动我的gps活动,我每5秒钟调用一次更新,并使用python API记录数据。我是android开发的新手,所以我真的可以使用一些帮助。
TripActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trip);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
final int width = size.x;
tripBtn = findViewById(R.id.tripBtn);
tripBtnWidth = tripBtn.getWidth();
String readToken = getTokenFromInternalStorage("utkn");
if(readToken == null) {
Intent intent = new Intent(TripActivity.this, MainActivity.class);
startActivity(intent);
}else{
token = readToken;
}
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
LocationListener mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
start = new LatLng(location.getLatitude(), location.getLongitude());
if(onTrip && tripId != 0) {
//LatLng previousCoords = start;
mMap.addCircle(new CircleOptions()
.center(start)
.radius(15)
.strokeColor(Color.argb(255, 90, 0, 175))
.fillColor(Color.argb(255, 90, 0, 175))
.strokeWidth(1)
);
/*
mMap.addPolyline(new PolylineOptions()
.add(previousCoords)
.add(start)
.color(Color.argb(255, 90, 0, 175))
);
*/
saveTripEntry(start);
}else{
mMap.clear();
}
}
@Override
public void onProviderEnabled(String provider) {
onTrip = false;
tripBtn.setWidth(tripBtnWidth);
tripBtn.setBackgroundResource(R.drawable.trip_button);
tripBtn.setText(R.string.trip_button);
gpsEnabled = true;
networkEnabled = true;
}
LocationManager mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000,
10, mLocationListener);
}
Location lastKnownLocation = null;
try {
gpsEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
lastKnownLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
} catch(Exception ex) {}
try {
networkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
lastKnownLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
} catch(Exception ex) {}
if (lastKnownLocation != null){
start = new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude());
}
tripBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!onTrip && (gpsEnabled || networkEnabled)) {
if(start != null) {
createTrip();
mMap.moveCamera(CameraUpdateFactory.newLatLng(start));
mMap.addCircle(new CircleOptions()
.center(start)
.radius(15)
.strokeColor(Color.argb(255, 90, 0, 175))
.fillColor(Color.argb(255, 90, 0, 175))
.strokeWidth(1)
);
saveTripEntry(start);
}
onTrip = true;
tripBtn.setWidth(width);
tripBtn.setBackgroundResource(R.drawable.cancel_trip_button);
tripBtn.setText(R.string.cancel_trip_button);
}else{
onTrip = false;
tripBtn.setWidth(tripBtnWidth);
tripBtn.setBackgroundResource(R.drawable.trip_button);
tripBtn.setText(R.string.trip_button);
mMap.clear();
}
if(!gpsEnabled){
Toast.makeText(TripActivity.this, "El GPS o WIFI no se encuentran habilitados", Toast.LENGTH_SHORT).show();
}
}
});
@Override
protected void onResume() {
super.onResume();
if (checkLocationPermission()) {
gpsTracker = new GPSTracker(this);
if (gpsTracker.canGetLocation) {
start = new LatLng(gpsTracker.getLatitude(), gpsTracker.getLongitude());
} else {
finish();
}
}
}
GPSTracker类
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
public boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 5; // 5 seconds
// Declaring a Location Manager
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
@SuppressLint("MissingPermission")
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
0, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
0, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
*/
public void stopUsingGPS() {
if (locationManager != null) {
locationManager.removeUpdates(GPSTracker.this);
}
}
/**
* Function to get latitude
*/
public double getLatitude() {
if (location != null) {
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
*/
public double getLongitude() {
if (location != null) {
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
/**
* Function to check GPS/wifi enabled
*
* @return boolean
*/
public boolean canGetLocation() {
return this.canGetLocation;
}
/**
* Function to show settings alert dialog
* On pressing Settings button will lauch Settings Options
*/
public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message_Preview
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message_Preview
alertDialog.show();
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}