Android GPS定位赢了';t更新
我想编程一个GPS定位器,每15秒更新一次位置。但是,要更新的方法(onLocationChanged)未调用。。。 当按下按钮时,它只设置文本视图中位置的一次时间,显示的时间有时也是在 确切地说,什么时候呼叫get-onLocationChanged?每10秒(或更快)强制一次,还是仅在位置更改时强制一次 我的代码(只有GPS的代码,还有检查google play服务是否激活的代码): 代码部分复制自Google开发者培训网站请尝试以下代码:-Android GPS定位赢了';t更新,android,gps,Android,Gps,我想编程一个GPS定位器,每15秒更新一次位置。但是,要更新的方法(onLocationChanged)未调用。。。 当按下按钮时,它只设置文本视图中位置的一次时间,显示的时间有时也是在 确切地说,什么时候呼叫get-onLocationChanged?每10秒(或更快)强制一次,还是仅在位置更改时强制一次 我的代码(只有GPS的代码,还有检查google play服务是否激活的代码): 代码部分复制自Google开发者培训网站请尝试以下代码:- public class 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;
boolean canGetLocation = false;
Location location; // location
public static double latitude; // latitude
public static double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
// Declaring a Location Manager
protected LocationManager locationManager;
public GPSTracker(Context context)
{
this.mContext = context;
getLocation();
}
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,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
// Log.d("Network", "Network");
if (locationManager != null)
{
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null)
{
latitude = location.getLatitude();
longitude = location.getLongitude();
// System.out.println("latitude if (isNetworkEnabled) => "+latitude);
// System.out.println("longitude if (isNetworkEnabled) => "+longitude);
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled)
{
if (location == null)
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, 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();
// System.out.println("latitude if (isGPSEnabled) => "+latitude);
// System.out.println("longitude if (isGPSEnabled) => "+longitude);
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
@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;
}
}
当您需要lat和long时,请按如下方式呼叫:-
new GPSTracker();
System.out.println(GPSTracker.latitude + GPSTracker.longitude);
我不知道错误是什么,但现在它工作了。谢谢您!
new GPSTracker();
System.out.println(GPSTracker.latitude + GPSTracker.longitude);