getLastKnownLocation 第一次 return 空问题
getLastKnownLocation first time return null issue
我正在开发一个简单的 android 位置应用程序,但我有点小麻烦。(也很抱歉我的英语不好。)
我想通过单击按钮获取当前位置,但 getLastKnownLocation returns 第一次全部为空。
而且,如果我首先打开 google 地图并在地图上显示我的当前位置,然后我通过背景,然后打开我自己的应用程序,请单击可用的按钮。但我不想要这种方式,我只想在我的应用程序上提供位置而不是这种方式。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Object clipboardService = getSystemService(CLIPBOARD_SERVICE);
final ClipboardManager clipboardManager = (ClipboardManager) clipboardService;
tvEnlem = (TextView) findViewById(R.id.textViewEnlem);
tvBoylam = (TextView) findViewById(R.id.textViewBoylam);
retrieveLocationButton = (Button) findViewById(R.id.buttonNeredeyim);
locationManager = (LocationManager) getSystemService(Context.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) {
// TODO: Consider calling
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION}, 101);
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
retrieveLocationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showCurrentLocation();
}
});
}
protected void showCurrentLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION}, 101);
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(MainActivity.this, message,
Toast.LENGTH_LONG).show();
this.tvBoylam.setText(String.valueOf(location.getLongitude()));
this.tvEnlem.setText(String.valueOf(location.getLatitude()));
}
else{
Toast.makeText(MainActivity.this,"LOKASYON BİLGİLERİ BOŞ",
Toast.LENGTH_SHORT).show();
}
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(MainActivity.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(MainActivity.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(MainActivity.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
}
}
如何获取没有空值的位置?
这不是错误,而只是 Google 政治。
getLastKnowLocation() 方法 return google 服务检索的最后一个位置。
正如 Google 文档所说:
The location object may be null in the following situations:
- Location is turned off in the device settings. The result could be null even if the last location was previously retrieved because
disabling location also clears the cache.
- The device never recorded its location, which could be the case of a new device or a device that has been restored to factory
settings.
- Google Play services on the device has restarted, and there is no active Fused Location Provider client that has requested
location after the services restarted.
这就是为什么如果您从 Google 地图切换到您的应用程序,您可以获得最后的位置。
要在不从应用程序传递到其他应用程序的情况下获得实际位置,您需要实现一个新的客户端并自行请求位置更新。有关详细信息,请参阅 Receiving Location Updates。
我希望我已经澄清了你的每一个疑问。否则,请不要犹豫写:)
我正在开发一个简单的 android 位置应用程序,但我有点小麻烦。(也很抱歉我的英语不好。)
我想通过单击按钮获取当前位置,但 getLastKnownLocation returns 第一次全部为空。
而且,如果我首先打开 google 地图并在地图上显示我的当前位置,然后我通过背景,然后打开我自己的应用程序,请单击可用的按钮。但我不想要这种方式,我只想在我的应用程序上提供位置而不是这种方式。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Object clipboardService = getSystemService(CLIPBOARD_SERVICE);
final ClipboardManager clipboardManager = (ClipboardManager) clipboardService;
tvEnlem = (TextView) findViewById(R.id.textViewEnlem);
tvBoylam = (TextView) findViewById(R.id.textViewBoylam);
retrieveLocationButton = (Button) findViewById(R.id.buttonNeredeyim);
locationManager = (LocationManager) getSystemService(Context.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) {
// TODO: Consider calling
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION}, 101);
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
retrieveLocationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showCurrentLocation();
}
});
}
protected void showCurrentLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION}, 101);
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(MainActivity.this, message,
Toast.LENGTH_LONG).show();
this.tvBoylam.setText(String.valueOf(location.getLongitude()));
this.tvEnlem.setText(String.valueOf(location.getLatitude()));
}
else{
Toast.makeText(MainActivity.this,"LOKASYON BİLGİLERİ BOŞ",
Toast.LENGTH_SHORT).show();
}
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(MainActivity.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(MainActivity.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(MainActivity.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
}
}
如何获取没有空值的位置?
这不是错误,而只是 Google 政治。 getLastKnowLocation() 方法 return google 服务检索的最后一个位置。 正如 Google 文档所说:
The location object may be null in the following situations:
- Location is turned off in the device settings. The result could be null even if the last location was previously retrieved because
disabling location also clears the cache.- The device never recorded its location, which could be the case of a new device or a device that has been restored to factory settings.
- Google Play services on the device has restarted, and there is no active Fused Location Provider client that has requested location after the services restarted.
这就是为什么如果您从 Google 地图切换到您的应用程序,您可以获得最后的位置。 要在不从应用程序传递到其他应用程序的情况下获得实际位置,您需要实现一个新的客户端并自行请求位置更新。有关详细信息,请参阅 Receiving Location Updates。
我希望我已经澄清了你的每一个疑问。否则,请不要犹豫写:)