2017-08-04 53 views
1

我想在后台服务中使用FusedLocation API。我对收到错误: -FusedLocationAPI在后台服务

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleAPIClient, mLocationRequest, this); 

问我的第三个参数浇铸成(转换成com.google.android.gms.location.LocationListener)。 我试图这样做,应用程序崩溃。我没有任何想法为什么发生这种情况或要求投。

所述服务类完整的代码如下: -

public class LocationBgService extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener { 

private GoogleApiClient mGoogleAPIClient; 
private LocationRequest mLocationRequest; 
private FusedLocationProviderApi mLocationProvider; 

@Nullable 
@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 

    mGoogleAPIClient = new GoogleApiClient.Builder(this) 
      .addApi(LocationServices.API) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .build(); 

    mLocationProvider = LocationServices.FusedLocationApi; 

    mLocationRequest = new LocationRequest(); 
    mLocationRequest.setInterval(60000); 
    mLocationRequest.setFastestInterval(15000); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    mGoogleAPIClient.connect(); 
    return START_STICKY; 
} 

@Override 
public void onConnected(@Nullable Bundle bundle) { 
    RequestLocationUpdates(); 
} 

private void RequestLocationUpdates() { 
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

    } 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleAPIClient, mLocationRequest, this); //getting error here..for casting..! 

} 

@Override 
public void onConnectionSuspended(int i) { 

} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

} 

@Override 
public void onLocationChanged(Location location) { 
    Log.d("Background Location ", "::::***********Latitude: " + location.getLatitude() + " Longitude: " + location.getLongitude()); 
} 

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

} 

@Override 
public void onProviderEnabled(String provider) { 

} 

@Override 
public void onProviderDisabled(String provider) { 

} 
} 

我搜索,发现很多人都在使用相同的代码行,为什么我得到这个错误。 我错过了什么或做什么?请帮助我这个。感谢提前。

回答

2

您可能正在使用旧版本的LocationListener。看起来您正在使用专门用于LocationManager的工具,而不是较新的LocationServices API。

在import语句,改变

import android.location.LocationListener; 

对此

import com.google.android.gms.location.LocationListener; 

你需要在底部取出3种方法。它们不在新的LocationListener中使用。另外,你可能需要清理&做一个完整的重建。使用新的导入语句,您的代码适用于我。

+0

Thankz man.Such a blunder。我实现了这个'com.google.android.gms.location.LocationListener'并且工作正常。还有一件事。当GPS关闭时,上面的代码可以获得使用网络提供商的用户的位置。至于现在我只有在GPS打开时才能获取用户的位置。我在Manifest文件中添加了FINE&COARSE权限。我需要添加其他东西吗? –