Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么我的OnLocationChanged()从未被调用?_Android_Android Location_Android Loadermanager - Fatal编程技术网

Android 为什么我的OnLocationChanged()从未被调用?

Android 为什么我的OnLocationChanged()从未被调用?,android,android-location,android-loadermanager,Android,Android Location,Android Loadermanager,我想做什么: 我正在尝试开发一个只需要在一个活动开始时提供用户位置的应用程序。因此,只有当用户在活动中时,位置才能通过网络或GPS进行更新。因此,用户可以选择室内地图 我的问题是什么: public final class LocationDetector implements LocationListener { private final Context mContext; private boolean isNetworkEnabled = false; pri

我想做什么:

我正在尝试开发一个只需要在一个活动开始时提供用户位置的应用程序。因此,只有当用户在活动中时,位置才能通过网络或GPS进行更新。因此,用户可以选择室内地图

我的问题是什么:

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) {

    }

}
但是,我发现应用程序总是使用历史位置,并且从不更新位置。我怀疑我的电脑一定出了问题

location=locationManager.getLastKnownLocation(locationManager.NETWORK\u提供程序)

但我不确定问题出在哪里

相关代码片段:

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) {

    }

}
在我的
活动中
,我有:

    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();
    }
My
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
,这意味着它从未被调用过!
同样从地图上,我可以看到位置没有更新。

没错,您可能会面临硬件问题。请在设备上安装App:GPS STATUS,以查看您的GPS是否正常工作

locationManager.RequestLocationUpdate (LocationManager.GPS\u提供程序、最短时间更新、最短距离更新、最短距离更新)

这条线

更新的最小距离更改=0米

最短时间更新=1000秒

关于您的问题,如果当前位置更新与上次已知位置不匹配,将调用onLocationChanged()

更新的位置将每分钟更改一次(在我的情况下为1000毫秒),并且如果设备移动了minDistance(在我的情况下为0米)距离,也会更改


我希望你能理解这一点。

因为这似乎是获取Android位置的常见问题,我将列出一个常见修复的快速清单:


  • 检查你的舱单

    最常见的问题之一是从未授予正确的权限。如果您使用的是GPS(带或不带网络),请使用
    ,否则请使用
    。谷歌的FusedLocationApi需要
    访问\u FINE\u位置


  • (适用于Android 6+)

    检查并请求权限!如果您从未被授予权限,那么最终将导致崩溃,或者更糟的是(如果您捕获了所有异常),最终将没有任何迹象显示任何情况!无论用户是否在应用程序开始时授予您权限,始终检查您是否拥有所有呼叫的权限。用户可以轻松地转到其设置并撤销它们


  • 仔细检查你的代码

    你确定你传递的是正确的听众吗?您是否将
    BroadcastReceiver
    IntentService
    添加到您的清单中?您是在
    BroadcastReceiver
    类上使用
    pendingent.getService()
    ,还是在
    IntentService
    类上使用
    getBroadcast()
    ?您确定没有在请求后立即在代码的其他地方注销侦听器吗


  • 检查设备设置

    显然,请确保已打开位置服务

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

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


  • 仔细检查你的代码

    是的,这个在这里放了两次。您是否尝试使用
    LocationListener
    而不是
    PendingEvent
    ,或者反过来使用
    LocationManager
    ,以确保实际正确实施
    LocationManager?您确定在活动或服务生命周期的某些部分中没有删除位置请求,而这是您不希望发生的吗


  • 检查你周围的环境

    你在旧金山中部一楼的GPS上测GPS吗?你在无处测试网络位置吗?你是否在一个没有任何无线电信号的秘密地下掩体里工作,想知道为什么你的设备无法获取位置?在解决位置问题时,请始终仔细检查周围环境



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

    (1000*60*0.5)
    这实际上是30秒,您需要将其更改为
    (1000*60*5)
    ,间隔5分钟,然而,这不是实际问题。您在清单中授予了足够的权限吗?@Vigbyor-我猜这5分钟是关于
    GPS\u提供程序的
    ,因为它们对于
    网络\u提供程序
    ?好的,现在评论此代码
    如果(locationManager!=null){location=locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);}
    并再次尝试执行您的代码。@perfectionm1ng-您可能会得到GPS\u提供程序刷新的位置,但由于您给它30秒的刷新时间,所以这永远都不够,这就是为什么它不刷新它。另一方面,您是
    getLastKnownLocation(LocationManager.NETWORK\u提供程序)
    ,它永远不会刷新,因为首选的提供商是GPS-对吗?