我一直看到GPS图标是什么?

时间:2015-04-15 00:36:44

标签: android gps locationmanager

我已经编写了自定义位置管理器,每30秒检查一次用户位置的更新。代码工作正常,即我正在接收用户位置的更新。但问题是GPS图标始终显示在顶部的状态栏上。我猜它应该在30秒内只能看一次。这是正常的还是我做错了什么?

enter image description here

public volatile Double mLatitude = 0.0;
public volatile Double mLongitude = 0.0;

int minTime = 30000;
float minDistance = 0;
MyLocationListener myLocListener = new MyLocationListener();
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

Criteria criteria = new Criteria();
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setSpeedRequired(false);

String bestProvider = locationManager.getBestProvider(criteria, false);
locationManager.requestSingleUpdate(criteria, myLocListener, null);     
locationManager.requestLocationUpdates(bestProvider, minTime, minDistance, myLocListener);


private class MyLocationListener implements LocationListener {
        @Override
        public void onLocationChanged(Location loc)
        {
            if (loc != null) {
                //Do something knowing the location changed by the distance you requested
                mLatitude = loc.getLatitude();
                mLongitude = loc.getLongitude();
                Toast.makeText(mContext, "Location Changed! "+Double.toString(mLatitude)+" "+Double.toString(mLongitude), Toast.LENGTH_LONG).show();
            }

        }

        @Override
        public void onProviderDisabled(String arg0)
        {
            //Do something here if you would like to know when the provider is disabled by the user
            Toast.makeText(mContext, "Provider Disabled! "+Double.toString(mLatitude)+" "+Double.toString(mLongitude), Toast.LENGTH_LONG).show();

        }

        @Override
        public void onProviderEnabled(String arg0)
        {
            //Do something here if you would like to know when the provider is enabled by the user
            Toast.makeText(mContext, "Provider Enabled! "+Double.toString(mLatitude)+" "+Double.toString(mLongitude), Toast.LENGTH_LONG).show();

        }

        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2)
        {
            //Do something here if you would like to know when the provider status changes
            Toast.makeText(mContext, "Provider Status Changed! "+Double.toString(mLatitude)+" "+Double.toString(mLongitude), Toast.LENGTH_LONG).show();

        }
    }

2 个答案:

答案 0 :(得分:4)

只要有1个或更多应用为GPS提供商调用requestLocationUpdates,GPS就会保持开启状态。它不会在请求之间关闭。它不能这样做会导致它失去卫星锁定,导致它必须重新建立。有时需要超过30秒。因此GPS将保持开启状态,直到您取消注册GPS事件。

答案 1 :(得分:0)

这个问题很老,但是仍然可以给出更好的答案。

您需要使用Handler,该间隔每5分钟连续运行一次,因此,您一次只能获取一次位置更新,然后释放GPS设备,这样您的应用将不会监听GPS更新,但您的处理程序知道何时应再次调用它以侦听更新。

public static void getLocation(Context context) {

    Handler locationHandler = new Handler();

    locationHandler.post(new Runnable() {
        @Override
        public void run() {
            LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    // Handle the location update
                }

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

                }

                @Override
                public void onProviderEnabled(String provider) {

                }

                @Override
                public void onProviderDisabled(String provider) {

                }
            }, null);

            locationHandler.postDelayed(this, 1000 * 60 * 5);
        }
    });
}