gpt4 book ai didi

android - 为什么我的 OnLocationChanged() 永远不会被调用?

转载 作者:可可西里 更新时间:2023-11-01 18:48:25 26 4
gpt4 key购买 nike

我正在尝试做的事情:

我正在尝试开发一款只需要在一项 Activity 开始时提供用户位置信息的应用。因此,只有当用户在 Activity 中时,位置才会通过网络或 GPS 更新。因此,用户可以选择室内地图。

我的问题是什么:

但是,我发现该应用程序始终使用历史位置,并且从不更新位置。我怀疑我的东西一定有问题

location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)

但我不确定问题出在哪里。

相关代码 fragment :

在我的 Activity 中,我有:

    locationDetector = new LocationDetector(MapSelectionActivity.this);
// try to get the current location
if (locationDetector.checkLocationServiceAvailability()) {
location = locationDetector.getLocation();
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
Log.d("MapSelectionActivity", latitude + " " + longitude);
//locationDetector.stopLocalization(); // stop the localization to save the energy
} else { // if no location service, requires the user to turn GPS on
locationDetector.showSettingsAlert();
}

我的LocationDetector类如下:

public final class LocationDetector implements LocationListener {

private final Context mContext;

private boolean isNetworkEnabled = false;
private boolean isGPSEnabled = false;
private boolean canGetLocation = false;

private Location location;
private String providerUsed;

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0; // 0 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = (long) (1000 * 60 * 0.5); // 0.5 minute

// Declaring a Location Manager
protected LocationManager locationManager;

// constructor
public LocationDetector(Context context) {

this.mContext = context;

locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
}

// NOTE call checkLocationServiceAvailability(); first before calling this!
public Location getLocation() {
// I SUSPECT SOMETHING IS WRONG HERE
if (isNetworkEnabled) { // use network

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("LocationDetector", "Using Network");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}

providerUsed = "Network";

} else if (isGPSEnabled) { // use GPS

if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("LocationDetector", "Using GPS");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}

providerUsed = "GPS";

} else { // neither the network nor the GPS is on

providerUsed = null;

Toast.makeText(mContext, "Location service is unavaliable", Toast.LENGTH_SHORT).show();
}

return location;
}

// call this to restart requesting the detecting
public void startLocalization() {

if (locationManager != null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
}
}

// call this to stop the detecting to save power
public void stopLocalization() {

if (locationManager != null) {
locationManager.removeUpdates(LocationDetector.this);
}
}

// check location service availability
public boolean checkLocationServiceAvailability() {

// check GPS on or off
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// check Internet access
ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
isNetworkEnabled = true;
} else {
isNetworkEnabled = false;
}

if (isGPSEnabled || isNetworkEnabled) {
canGetLocation = true;
} else {
canGetLocation = false;
}

return canGetLocation;
}

public String getLocationProvider() {

return providerUsed;
}

// show alert dialog to direct the users to the settings
public void showSettingsAlert() {

AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

// make it uncancellable
alertDialog.setCancelable(false);

// Setting Dialog Title
alertDialog.setTitle("Forgot to turn GPS on?");

// Setting Dialog Message
alertDialog.setMessage("Currently there is no Internet access.\n\nLocalization requires GPS when Internet is unavailiable.\n\nDo you want to enable GPS so as to proceed?");

// On pressing Settings button
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);

Toast.makeText(mContext, "After enabling GPS, press the physical 'Back' button to return", Toast.LENGTH_LONG).show();
}
});

// on pressing cancel button
alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();

Toast.makeText(mContext, "No location service, please choose map manually", Toast.LENGTH_LONG).show();
}
});

// Showing Alert Message
alertDialog.show();
}

@Override
public void onLocationChanged(Location _location) {
// IT NEVER GETS CALLED
location = _location;

// update the text view
MapSelectionActivity.coordinatesTextView.setText("(" + Math.round(location.getLatitude() * 1000) / 1000.0 + ", " + Math.round(location.getLongitude() * 1000) / 1000.0 + ")");

// update the marker on Google Maps
MapSelectionActivity.googleMap.clear();
MapSelectionActivity.googleMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("I am here!"));
MapSelectionActivity.googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 15)); // 15 is the approporiate zooming level

// re-suggest the map
int recommendedMapSequenceNumber = MapSelectionActivity.mapDatabase.getMapSequenceNumber(location.getLatitude(), location.getLongitude());
MapSelectionActivity.recommendedMapTextView.setTextColor(Color.parseColor("red"));
if (recommendedMapSequenceNumber == -1) { // the so-called nearest is still too far

Toast.makeText(mContext, "Please manually select one to proceed", Toast.LENGTH_LONG).show();
MapSelectionActivity.recommendedMapTextView.setText("No recommended maps");
MapSelectionActivity.autoSelectButton.setEnabled(false);
} else { // suggest a map

Toast.makeText(mContext, "One suitable map found", Toast.LENGTH_SHORT).show();
MapSelectionActivity.recommendedMapTextView.setText(MapSelectionActivity.mapDatabase.getMapName(recommendedMapSequenceNumber));
}

Toast.makeText(mContext, "New location detected", Toast.LENGTH_SHORT).show();
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

}

我从未在 onLocationChanged() 中看到 Toast,这意味着它永远不会被调用!同样从 map 上,我可以看到位置没有更新。

最佳答案

由于这似乎是获取 Android 位置的常见问题,我将列出一个常见修复的快速 list :


  1. 检查您的 list !

    最常见的问题之一是从未授予正确的权限。如果您使用 GPS(有或没有网络),请使用 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> , 否则使用 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> .谷歌的 FusedLocationApi 需要 ACCESS_FINE_LOCATION .


  1. (适用于 Android 6+)Check runtime permissions !

    检查并请求权限!如果您从未被授予权限,您将以崩溃告终,或者更糟(如果您正在捕获所有异常),您最终将没有任何指示!用户是否在应用程序启动时授予您权限并不重要,请始终检查您是否拥有所有调用的权限。用户可以轻松转到他们的设置并撤销它们。


  1. 仔细检查您的代码!

    你确定你传递的是正确的监听器吗?你加了吗BroadcastReceiverIntentService到你的 list ?你在使用 PendingIntent.getService()BroadcastReceiver 上类,或 getBroadcast()IntentService 上类(class)?您确定您没有在请求后立即在代码中的其他地方取消注册您的监听器吗?


  1. 检查设备设置!

    显然,请确保您已打开定位服务。

    enter image description here

    如果您正在使用网络服务,您是否打开了“始终可用扫描”?您是否将定位模式设置为“最佳”(“高精度”)或“省电”(“仅网络”)?

    enter image description here

    如果您使用的是 GPS,您是否在定位模式中打开了“最佳”(“高精度”)或“仅限设备”?

    enter image description here


  1. 仔细检查您的代码!

    是的,这是在这里两次。您是否尝试使用 LocationListener而不是 PendingIntent ,反之亦然,以确保您实际实现了 LocationManager适本地?您确定位置请求没有在您不希望发生的 Activity 或服务生命周期的某些部分被删除吗?


  1. 检查周围环境!

    您是在旧金山市中心一栋大楼的一楼测试 GPS 吗?您是否在偏僻的地方测试网络位置?您是否在一个没有所有 radio 信号的 secret 地下掩体中工作,想知道为什么您的设备无法定位?尝试解决位置问题时,请务必仔细检查周围环境!


可能还有许多其他不太明显的原因导致位置不起作用,但在搜索那些深奥的修复程序之前,只需浏览一下这个快速 list 。

关于android - 为什么我的 OnLocationChanged() 永远不会被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17804699/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com