您好,我有一个连接在私人 APN 上的 Android 手机上的应用程序,如果我无法导航到不在我的 APN 上的任何地方,我想知道如何获取 GPS 位置。
这是在我的应用程序上使用 GPS 的代码:
public double[] _getLocation (Context _context)
{
// Get the location manager
LocationManager locationManager = (LocationManager)_context.getSystemService (LOCATION_SERVICE);
Criteria criteria = new Criteria ();
String bestProvider = locationManager.getBestProvider (criteria, true);
Location location = locationManager.getLastKnownLocation (bestProvider);
LocationListener loc_listener = new LocationListener() {
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
};
locationManager.requestLocationUpdates(bestProvider,0 ,0, loc_listener);
location = locationManager.getLastKnownLocation (bestProvider);
double lat=0.00;
double lon=0.00;
try
{
lat = location.getLatitude ();
lon = location.getLongitude ();
}
catch (NullPointerException e)
{
Toast.makeText(_context,"El GPS debe estar encendido, asegurate en configuracion-->Localizacion",Toast.LENGTH_LONG).show();
}
double[] result = {lat,lon};
return result;
}