0

我開發了一個使用Fused位置提供程序的應用程序。在onConnected()方法中,我請求位置更新,並且將啓動應用程序邏輯並調用onLocationChanged()OnLocationChanged()異常

問題onLocationChanged()方法不在美國的設備中調用。此代碼在印度的設備上可以很好地工作,但不適用於美國。通過不起作用,我的意思是locationClient獲得連接,但不會調用onLocationChanged()

下面的代碼:

public class LocationReceiver extends BroadcastReceiver 
     implements 
     // GooglePlayServicesClient.ConnectionCallbacks, 
     // GooglePlayServicesClient.OnConnectionFailedListener, 
     GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener, LocationListener { 
    // LocationClient locationclient = null; 
    GoogleApiClient locationclient = null; 
    Context contxt; 
    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     contxt = context; 
     // Log.i("locationreciever", "in location rec");, 
     Log.i("fused", "in location rec"); 

     int resp = GooglePlayServicesUtil 
       .isGooglePlayServicesAvailable(context); 

     if (resp == ConnectionResult.SUCCESS) { 
      // locationclient = new LocationClient(context, this, this); 
      locationclient = new GoogleApiClient.Builder(context) 
        .addApi(LocationServices.API) 
        .addConnectionCallbacks(this)// (mConnectionCallbacks) 
        .addOnConnectionFailedListener(this)// (mOnConnectionFailedListener) 
        .build(); 
      locationclient.connect(); 
     } else { 
      Log.i("fused", "loc client Google Play Service Error"); 
     } 
    } 

    public void updateTransientLocation(Context context, Location loc) { 
     // Log.i("updateTransientLocation", "in fn"); 

     float lat = (float) loc.getLatitude(); 
     float lon = (float) loc.getLongitude(); 
     float acc = loc.getAccuracy(); 
     float alt = (float) loc.getAltitude(); 

     if (lat > 0 && lon > 0) { 
      PreferenceForApp prefs = new PreferenceForApp(contxt); 
      prefs.setTransientLatitude(lat); 
      prefs.setTransientLongitude(lon); 
      prefs.setTransientAccuracy(acc); 
      prefs.setTransientAltitude(alt); 
     } 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     Log.i("fused", 
       " onLocationChanged Location Request :" 
         + location.getLatitude() + "," 
         + location.getLongitude() + " acc " 
         + location.getAccuracy()+" alt "+location.getAltitude()); 
     //TODO wait for some time to get location 
     updateTransientLocation(contxt, location); 
     if (locationclient != null) { 
      if (locationclient.isConnected()) { 
       // locationclient.removeLocationUpdates(this); 
       LocationServices.FusedLocationApi.removeLocationUpdates(
         locationclient, this); 

       locationclient.disconnect(); 
      } 
     } 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult arg0) { 
     PreferenceForApp prefs = new PreferenceForApp(contxt); 
//  if (arg0.hasResolution()) { 
//   try { 
//    // Start an Activity that tries to resolve the error 
//    arg0.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST); 
//   } catch (IntentSender.SendIntentException e) { 
//    e.printStackTrace(); 
//   }}else{ 
     Log.i("fused", "loc client connection failed"); 
     prefs.setGooglePlayServiceErrorCode(arg0.getErrorCode()); 
    } 
//} 

    @Override 
    public void onConnected(Bundle arg0) { 
     PreferenceForApp prefs = new PreferenceForApp(contxt); 
     prefs.setGooglePlayServiceErrorCode(0); 
     Log.i("fused", "loc client onConnected"); 
     LocationRequest locationrequest = new LocationRequest(); 
     locationrequest 
       .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 

//  PRIORITY_BALANCED_POWER_ACCURACY 
     // locationclient.requestLocationUpdates(locationrequest, this); 
     LocationServices.FusedLocationApi.requestLocationUpdates(
       locationclient, locationrequest, this); // mLocationListener); 
    } 

    // @Override 
    // public void onDisconnected() { 
    // Log.i("fused", "loc client disconnected"); 
    // } 

    @Override 
    public void onConnectionSuspended(int arg0) { 
     Log.i("fused", "loc client onConnectionSuspended"); 
    } 
} 

誰能幫我這個問題?有什麼我在這裏失蹤?

回答

0

也許這是事實,手機無法連接到服務提供商?

嘗試使用gps。

String locationProvider = LocationManager.GPS_PROVIDER; 
// Or, use GPS location data: 
// String locationProvider = LocationManager.NETWORK_PROVIDER; 
+0

如何在FusedLocationApi中使用位置管理器?通過使用FusedLocation我已經通過GPS –

0

我想你使用NETWORK_PROVIDER來訪問/獲取位置更新。

嘗試使用GPS_PROVIDER或使用PASSIVE_PROVIDER從任一NETWORK_PROVIDER或GPS_PROVIDER得到位置。

確保您擁有清單中的權限以訪問NETWORK和GPS位置。

+0

更新位置,但是我使用FusedLocationApi,它適用於所有(GPS_provider,Network_provider)。 –

0

如果您使用的是FusedLocationProviderApi,則可以選擇使用SettingsApi來檢查設備是否具有應用程序所需的位置設置。 SettingsApi和可選的提供一個位置對話框來更新設備的位置設置,如果它們被發現不合適的話。你可以看看example。在Settings中關閉位置運行示例,您應該看到對話框。您的應用可能失敗,因爲它沒有足夠的權限,位置對話框可能會有所幫助。