如何在服务中使用 Fuse android 位置提供程序?

Posted

技术标签:

【中文标题】如何在服务中使用 Fuse android 位置提供程序?【英文标题】:How to use Fuse android location provider in a service? 【发布时间】:2019-03-31 04:56:36 【问题描述】:

我正在尝试在 android 的服务中使用 Fuse 提供程序来获取后台位置。

  private void init() 
    context = this;
    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
    mSettingsClient = LocationServices.getSettingsClient(this);

    mLocationCallback = new LocationCallback() 
        @Override
        public void onLocationResult(LocationResult locationResult) 
            super.onLocationResult(locationResult);
            // location is received
            mCurrentLocation = locationResult.getLastLocation();
            mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());

            showLocation();
        
    ;

    mRequestingLocationUpdates = false;

    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
    mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
    builder.addLocationRequest(mLocationRequest);
    mLocationSettingsRequest = builder.build();


   private void startLocationUpdates() 
    mSettingsClient
            .checkLocationSettings(mLocationSettingsRequest)
     =====>>>>       .addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() 
                @SuppressLint("MissingPermission")
                @Override
                public void onSuccess(LocationSettingsResponse locationSettingsResponse) 
                    Log.i(TAG, "Started location updates!");
                    mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());

                     showLocation();
                
            )
    =====>>>>        .addOnFailureListener(this, new OnFailureListener() 
                @Override
                public void onFailure(@NonNull Exception e) 

                
            );

在服务中我不能使用“this”,因为它指的是一个活动。

我想知道如何在服务中实现这段代码?

【问题讨论】:

【参考方案1】:

我已经把它改成了这个,并且工作了......

    private void init() 
    context = this;
    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
    mSettingsClient = LocationServices.getSettingsClient(this);

    mLocationCallback = new LocationCallback() 
        @Override
        public void onLocationResult(LocationResult locationResult) 
            super.onLocationResult(locationResult);
            // location is received
            mCurrentLocation = locationResult.getLastLocation();
            mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());

            showLocation();
        
    ;

    mRequestingLocationUpdates = false;

    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
    mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
    builder.addLocationRequest(mLocationRequest);
    mLocationSettingsRequest = builder.build();
    



     private void startLocationUpdates() 
        mSettingsClient
                .checkLocationSettings(mLocationSettingsRequest)
         =====>>>>       .addOnSuccessListener(new OnSuccessListener<LocationSettingsResponse>() 
                    @SuppressLint("MissingPermission")
                    @Override
                    public void onSuccess(LocationSettingsResponse locationSettingsResponse) 
                        Log.i(TAG, "Started location updates!");
                        mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());

                     showLocation();
                
            )
    =====>>>>        .addOnFailureListener(new OnFailureListener() 
                @Override
                public void onFailure(@NonNull Exception e) 

                
            );

【讨论】:

以上是关于如何在服务中使用 Fuse android 位置提供程序?的主要内容,如果未能解决你的问题,请参考以下文章

nfs卡在fuse answer

如何在 Android 中使用后台服务每 5 分钟获取一次设备位置?

结合 fuse 和 rpc 来列出远程目录

FusedLocationProviderClient 和唤醒锁

JBoss Fuse - 如何使用不同的 CXF 版本

FUSE - 避免在 GetAttr 中计算大小