Android 获取用户位置一次
Android get User Location once
我知道这有很多问题,但即使我(几乎)全部阅读了它们,我仍然无法解决问题。
基本上,当我打开应用程序或按下按钮并保存到共享首选项时,我想获得用户的精确位置。
我的旧代码有效,但由于获取坐标的速度很慢,因此无法保存共享首选项(GPS 图标不显示在栏上)。
旧:
public void askLocation() {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
try {
mLocation = locationManager.getLastKnownLocation(provider);
} catch (SecurityException e) {
//
}
String currentCoordinates = somename.getString("LastLocation", "0,0");
if (mLocation != null) {
double currentLatitude = mLocation.getLatitude();
double currentLongitude = mLocation.getLongitude();
currentCoordinates = currentLatitude + "," + currentLongitude;
}
SharedPreferences.Editor editor = somename.edit();
editor.putString("LastLocation", currentCoordinates).commit();
Log.d("SomeName", "lastLocation start: " + currentCoordinates);
}
}
我从 Whosebug 上的 post 使用的新代码无法正常使用(GPS 图标显示在栏上):
public void askLocation() {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(true);
criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH);
// Now create a location manager
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Looper looper = null;
locationManager.requestSingleUpdate(criteria, locationListener, looper);
String currentCoordinates = somename.getString("LastLocation", "0,0");
Log.d("SomeName", "lastLocation askLocation: " + currentCoordinates);
}
}
final LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
mLocation = location;
String currentCoordinates = somename.getString("LastLocation", "0,0");
if (location != null) {
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
currentCoordinates = currentLatitude + "," + currentLongitude;
}
SharedPreferences.Editor editor = somename.edit();
editor.putString("LastLocation", currentCoordinates).commit();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Status Changed", String.valueOf(status));
}
@Override
public void onProviderEnabled(String provider) {
Log.d("Provider Enabled", provider);
}
@Override
public void onProviderDisabled(String provider) {
Log.d("Provider Disabled", provider);
}
};
谢谢。
您可以尝试使用此方法在打开应用程序或按下按钮时获取一次位置并保存在共享首选项中。
if (Constants.isOnline(getApplicationContext())) {
gps = new GPSTracker(v.getContext(),this);
if (gps.canGetLocation()) {
latitude = gps.getLatitude();
longitude = gps.getLongitude();
Log.e("Location ", "latitude : " + latitude + "longitude :" + longitude);
} else {
gps.showSettingsAlert();
}
}
而 isOnline() 是检查用户是否有互联网连接。
public static boolean isOnline(Context c) {
ConnectivityManager cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
} else {
Toast.makeText(c, "No Internet Connection.", Toast.LENGTH_SHORT).show();
}
return false;
}
我知道这有很多问题,但即使我(几乎)全部阅读了它们,我仍然无法解决问题。
基本上,当我打开应用程序或按下按钮并保存到共享首选项时,我想获得用户的精确位置。 我的旧代码有效,但由于获取坐标的速度很慢,因此无法保存共享首选项(GPS 图标不显示在栏上)。
旧:
public void askLocation() {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
try {
mLocation = locationManager.getLastKnownLocation(provider);
} catch (SecurityException e) {
//
}
String currentCoordinates = somename.getString("LastLocation", "0,0");
if (mLocation != null) {
double currentLatitude = mLocation.getLatitude();
double currentLongitude = mLocation.getLongitude();
currentCoordinates = currentLatitude + "," + currentLongitude;
}
SharedPreferences.Editor editor = somename.edit();
editor.putString("LastLocation", currentCoordinates).commit();
Log.d("SomeName", "lastLocation start: " + currentCoordinates);
}
}
我从 Whosebug 上的 post 使用的新代码无法正常使用(GPS 图标显示在栏上):
public void askLocation() {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(true);
criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH);
// Now create a location manager
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Looper looper = null;
locationManager.requestSingleUpdate(criteria, locationListener, looper);
String currentCoordinates = somename.getString("LastLocation", "0,0");
Log.d("SomeName", "lastLocation askLocation: " + currentCoordinates);
}
}
final LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
mLocation = location;
String currentCoordinates = somename.getString("LastLocation", "0,0");
if (location != null) {
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
currentCoordinates = currentLatitude + "," + currentLongitude;
}
SharedPreferences.Editor editor = somename.edit();
editor.putString("LastLocation", currentCoordinates).commit();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Status Changed", String.valueOf(status));
}
@Override
public void onProviderEnabled(String provider) {
Log.d("Provider Enabled", provider);
}
@Override
public void onProviderDisabled(String provider) {
Log.d("Provider Disabled", provider);
}
};
谢谢。
您可以尝试使用此方法在打开应用程序或按下按钮时获取一次位置并保存在共享首选项中。
if (Constants.isOnline(getApplicationContext())) {
gps = new GPSTracker(v.getContext(),this);
if (gps.canGetLocation()) {
latitude = gps.getLatitude();
longitude = gps.getLongitude();
Log.e("Location ", "latitude : " + latitude + "longitude :" + longitude);
} else {
gps.showSettingsAlert();
}
}
而 isOnline() 是检查用户是否有互联网连接。
public static boolean isOnline(Context c) {
ConnectivityManager cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
} else {
Toast.makeText(c, "No Internet Connection.", Toast.LENGTH_SHORT).show();
}
return false;
}