如何在 Service 类中使用 Fused Location Provider?
Posted
技术标签:
【中文标题】如何在 Service 类中使用 Fused Location Provider?【英文标题】:How can I use Fused Location Provider inside a Service class? 【发布时间】:2016-07-19 05:07:59 【问题描述】:我需要在后台获取用户的位置,所以我尝试在我的 Service 类中使用 Fused Location Provider。我将下面的代码放在onStart()
方法中,但无法识别上下文MainActivity.getAppContext()
。既然我在我的服务类中使用它,我可以用什么来代替它?
GoogleApi = new GoogleApiClient.Builder(MainActivity.getAppContext())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
【问题讨论】:
【参考方案1】:下面的代码解释了如何在服务中使用融合的位置提供者
import android.Manifest;
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
public class LocationService extends Service implements
LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener
private String TAG = LocationService.class.getSimpleName();
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private static final long INTERVAL = 1000 * 30 * 60;
private static final long FASTEST_INTERVAL = 1000 * 25 * 60;
private static final long MEDIUM_INTERVAL = 1000 * 30 * 60;
public LocationService()
@Override
public void onCreate()
super.onCreate();
createLocationRequest();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
@Override
public int onStartCommand(Intent intent, int flags, int startId)
if (!mGoogleApiClient.isConnected())
mGoogleApiClient.connect();
else
startLocationUpdates();
return START_STICKY;
@Override
public IBinder onBind(Intent intent)
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
protected void createLocationRequest()
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(MEDIUM_INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
@Override
public void onConnected(@Nullable Bundle bundle)
Log.v(TAG, "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected());
if (mGoogleApiClient.isConnected())
startLocationUpdates();
protected void startLocationUpdates()
if (mGoogleApiClient != null)
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
return;
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
Log.v(TAG, "Location update started ..............: ");
protected void stopLocationUpdates()
if (mGoogleApiClient != null && mGoogleApiClient.isConnected() && LocationServices.FusedLocationApi != null)
LocationServices.FusedLocationApi.removeLocationUpdates(
mGoogleApiClient, this);
Log.v(TAG, "Location update stopped .......................");
mGoogleApiClient.disconnect();
@Override
public void onConnectionSuspended(int i)
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult)
@Override
public void onLocationChanged(Location location)
Log.v(TAG, "Latitude: " + location.getLatitude() + " Longitude: " + location.getLongitude());
@Override
public void onDestroy()
super.onDestroy();
stopLocationUpdates();
Log.v(TAG, "Service Stopped!");
【讨论】:
FusedLocationApi 已弃用。推荐 FusedLocationProviderClient。 FusedLocationApi 类已弃用,但感谢您节省了我的时间。【参考方案2】:Service
是 Context
,因此您可以在 Service
中使用 this
。
Service 还有getApplicationContext()
,它返回应用程序的上下文,这看起来就像MainActivity.getAppContext()
应该返回的那样。你也可以使用它。
GoogleApi = new GoogleApiClient.Builder(getApplicationContext())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
【讨论】:
以上是关于如何在 Service 类中使用 Fused Location Provider?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 FUSED LOCATION API 优化电池 - Android
关于如何在 android 的安装类中使用 Rest API(service) 传递 ParseObject(Object)?
如何使用 Fused Location 订阅提供者状态更改事件?
spring3 mvc的service如何在类中的中的怎么样使用