setMockMode 不起作用 - FusedLocationApi

Posted

技术标签:

【中文标题】setMockMode 不起作用 - FusedLocationApi【英文标题】:setMockMode doesn't work - FusedLocationApi 【发布时间】:2017-09-05 20:08:44 【问题描述】:

我正在设置模拟位置(使用FusedLocationApi)以基于地图测试我的应用程序,但它似乎不起作用。

LocationServices.FusedLocationApi.setMockMode(mGoogleApiClient, true);


 Location location = new Location("fused");
            // Time is needed to create a valid Location
            long currentTime = System.currentTimeMillis();
            long elapsedTimeNanos = 0;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) 
                elapsedTimeNanos = SystemClock.elapsedRealtimeNanos();
                location.setElapsedRealtimeNanos(elapsedTimeNanos);
            
            location.setTime(currentTime);
            // new york 40.7128° N, 74.0059° W
            location.setLatitude(40.7128);
            location.setLongitude(74.0059);
            PendingResult<Status> s =  LocationServices.FusedLocationApi.setMockLocation(mGoogleApiClient, location);
            s.setResultCallback(new ResolvingResultCallbacks<Status>(MainActivity.this, 100) 
                @Override
                public void onSuccess(@NonNull Status status) 
                    if(status.isSuccess())
                        log("mock success");

                        if (mIsFromContinuousUpdates) 
                            startContinuousUpdates();
                            return;
                        
                        getLastKnowLocation();
                    
                

                @Override
                public void onUnresolvableFailure(@NonNull Status status) 

                
            );

PendingResult 返回成功,但是当我调用 getLastKnownLocation 时,它返回 null 并且当我请求持续更新时不会触发 onLocationChanged

我这里有什么遗漏吗?

【问题讨论】:

有答案吗? ..... 【参考方案1】:

当开始位置更新时,它应该收到一个实现 OnHandleIntent 的 pendingIntent,它从 Intent 参数中提取位置

public class LocationUpdateActivity extends IntentService implements LocationListener 
private static final String TAG = LocationUpdateActivity .class.getSimpleName();
private Location mCurrentLocation;

public LocationUpdateActivity(String name) 
    super(name);

public LocationUpdateActivity() 
    super("LocationUpdateActivity");


@Override
protected void onHandleIntent(@Nullable Intent intent) 
    Log.i(TAG, "onHandleIntent");
    if (LocationResult.hasResult(intent)) 
        LocationResult locationResult = LocationResult.extractResult(intent);
        mCurrentLocation = locationResult.getLastLocation();
        if (mCurrentLocation != null) 
            Log.d(TAG, "accuracy: " + mCurrentLocation.getAccuracy() + " lat: " + mCurrentLocation.getLatitude() + " lon: " + mCurrentLocation.getLongitude());
        
    

并设置更新:

Intent locationIntent = new Intent(mContext, LocationUpdateActivity.class);
PendingIntent locationPendingIntent = PendingIntent.getService(
                            mContext,
                            0,
                            locationIntent,
                            PendingIntent.FLAG_UPDATE_CURRENT
                    );

FusedLocationApi.requestLocationUpdates(
                            mGoogleApiClient, mLocationRequest, locationPendingIntent);

【讨论】:

不起作用。我什至在Fused Provider 都找不到这个方法

以上是关于setMockMode 不起作用 - FusedLocationApi的主要内容,如果未能解决你的问题,请参考以下文章

PHP.INI不起作用

C#DataGridView的行列表头背景色字体色不起作用

关于Animate css不起作用的原因

修改php.ini不起作用是为啥

Vba窗体的keydown怎么不起作用

Angular 的 $http.post 不起作用,它的 $http... 也不起作用,但 jQuerys ajax 起作用。为啥?