仅在启用GPS后,FusedLocationApi.requestLocationUpdates才会触发调用onLocationChanged

时间:2016-07-25 03:08:51

标签: android google-play-services locationmanager

在花了很多时间进行实验并尝试多种通话和设置组合之后,我缩小到这个范围:

Google Play服务位置API FusedLocationApi.requestLocationUpdates理想情况下,在启用任何位置服务时,应在指定的时间间隔内调用onLocationChanged。但是,实际行为是如此:

  1. 如果关闭所有位置服务,则永远不会调用onLocationChanged .......预期的行为
  2. 如果只启用了wifi /移动网络位置服务,则永远不会调用onLocationChanged .......意外行为
  3. 如果只启用了GPS定位服务,它就永远不会调用onLocationChanged(即使我们之后打开wifi定位服务).......意外行为
  4. 如果GPS位置服务开启(打开第一个wifi /移动定位服务后),它会拨打onLocationChanged(并在我们之后关闭GPS后继续通话).......预期的行为
  5. 如果打开wifi位置服务(打开第一个GPS位置服务后),它永远不会调用onLocationChanged .......意外行为
  6. 简而言之:只有先打开wifi然后再打开GPS,才会调用onLocationChanged。 (但是,之后不再需要GPS来呼叫onLocationChanged

    这是我的代码:

    void ConnectToLocationApi()
    {
        // Create an instance of GoogleAPIClient.
        if(m_GoogleApiClient == null)
        {
            // Initialize Google Api client
            m_GoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .build();
    
            // Connect to Google play service location api
            m_GoogleApiClient.connect();
        }
    }
    
    
    @Override
    public synchronized void onConnected(Bundle bundle) // Called when m_GoogleApiClient.connect() succeeds
    {
        Log.d(TAG, "GoogleApiClient connected");
    
        try
        {
            LocationRequest locationRequest;
    
            // Initialize location request
            locationRequest = new LocationRequest();
            locationRequest.setInterval(LOCATION_UPDATE_INTERVAL_IN_MILLISECONDS);
            locationRequest.setFastestInterval(FASTEST_LOCATION_UPDATE_INTERVAL_IN_MILLISECONDS);
            locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    
            // Start receiving location updates
            LocationServices.FusedLocationApi.requestLocationUpdates(m_GoogleApiClient, locationRequest, this);
    
            // Get location details of last available location (if any)
            Location lastLocation = LocationServices.FusedLocationApi.getLastLocation(m_GoogleApiClient);
    
            if (lastLocation != null)
            {
                ShowToast(this, "Last location received", Toast.LENGTH_SHORT);
                Log.i(TAG, "In onConnected. Latitude:" + m_Location.getLatitude() + ", Longitude: " + m_Location.getLongitude());
            }
            else
            {
                Log.e(TAG, "getLastLocation returned NULL value");
            }
        }
        catch (SecurityException e)
        {
            Log.e(TAG, "getLastLocation failed because of security exception (permission issue?)");
        }
    }
    
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) // Called when m_GoogleApiClient.connect fails
    {
        ShowToast(this, "GoogleApiClient connection failed", Toast.LENGTH_SHORT);
        m_GoogleApiClient = null;
        Log.e(TAG, "GoogleApiClient connection failed");
    }
    
    @Override
    public void onConnectionSuspended(int i) // Called when m_GoogleApiClient.connect suspended
    {
        Log.e(TAG, "GoogleApiClient connection suspended");
    }
    
    @Override
    public synchronized void onLocationChanged(Location location)
    {
        ShowToast(this, "Location update received", Toast.LENGTH_SHORT);
        Log.i(TAG, "In location changed. Latitude:" + location.getLatitude() + ", Longitude: " + location.getLongitude());
    }
    

    注意:在阅读this后,我更喜欢Android框架位置API上的Google Play服务位置API。有什么线索吗?我被困住了。

1 个答案:

答案 0 :(得分:0)

在我的情况下,问题在于倾听。 如果我在获得位置和gps的预先安排之前启动了GoogleApi服务,那么OnLocationChanged从未调用过。所以 在OnPermissionGrantedResult和reciver for android.location.PROVIDERS_CHANGED我正在重启GoogleApiServices并开始重新定位位置事件