在我当前的应用程序中,返回的位置数据已过时。我使用一种基于更新之间的最小时间/距离获取更新的方法。但是,假设我关闭手机,开车到另一个城市,然后重新打开手机。在这种情况下,我的 GPS 读数将关闭。我如何强制应用程序获取当前位置而不是 getLastKnownLocation()。
我听说过 locationListener,但我读过的所有内容都对如何使用它非常模糊。
这是我的代码,以防万一这澄清了发生了什么:
public class GPSHandling extends Service implements LocationListener{
private final Context myContext;
//flag for gps status
public boolean isGPSEnabled = false;
//flag for network status
public boolean isNetworkEnabled = false;
// used to determine if i can get a location either by network or GPS
public boolean canGetLocation = false;
Location myloc;
public double latitude;
public double longitude;
public int MIN_TIME_BTWN_UPDATE = 500*10; // in miliseconds, so 10sec
public int MIN_DISTANCE_BTWN_UPDATE = 10; // in meters
protected LocationManager locManager;
public GPSHandling(Context context){
this.myContext= context;
getLocation();
}
public Location getLocation(){
try{
locManager =(LocationManager) myContext.getSystemService(LOCATION_SERVICE);
// now get gps status
isGPSEnabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
//now the same for network
isNetworkEnabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled){
// do nothing since neither provider are enabled (this.cangetlocation = false but its already set to that by default)
}
else{
this.canGetLocation=true;
//first get values for locManager from the network provider. send parameters telling when to update
if(isNetworkEnabled){
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BTWN_UPDATE, MIN_DISTANCE_BTWN_UPDATE, this);
Log.d("provider", "network");
//if we are successful, then check to see if the location manager isnt null. attempt to get the current location from the manager
if (locManager != null){
myloc =locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
// after getting the current location, attempt to get the latitude and longitude values
if (myloc != null){
latitude = myloc.getLatitude();
longitude = myloc.getLongitude();
}
}
}
//now get values for locManager from the GPS provider. send parameters telling when to update
if(isGPSEnabled){
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BTWN_UPDATE, MIN_DISTANCE_BTWN_UPDATE, this);
Log.d("provider", "GPS");
}
//if we are successful, then check to see if the location manager isnt null. attempt to get the current location from the manager
if(locManager!= null){
myloc = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
// after getting the current location, attempt to get the latitude and longitude values
if(myloc != null){
latitude = myloc.getLatitude();
longitude = myloc.getLongitude();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return myloc;
}
// get an update of the current latitude by using this class method
public double getMyLatitude(){
if (myloc!= null){
latitude = myloc.getLatitude();
}
return latitude;
}
//get an update of the current longitude by using this class method
public double getMyLongitude(){
if (myloc != null ){
longitude = myloc.getLongitude();
}
return longitude;
}
// use this method to find if app can get the current location
public boolean canGetMyLocation(){
return this.canGetLocation;
}
public void showGPSDialog(){
AlertDialog.Builder alert = new AlertDialog.Builder(myContext);
// Setting Dialog Title
alert.setTitle("Location Setting");
// Setting Dialog Message
alert.setMessage("GPS is not enabled, do you want to enable this now in the settins menue?");
// setting icon to dialog
//alert.setIcon(R.drawable.)
// on pressing settings button
alert.setPositiveButton("Settins", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
myContext.startActivity(intent);
}
});
//on pressing cancel button
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// showing the alert dialog
alert.show();
}
public void stopUsingGPS(){
if(locManager !=null){
locManager.removeUpdates(GPSHandling.this);
}
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}