在FusedLocation中接收模拟位置

Mar*_*usz 2 android

我设置了模拟位置,但是在启用模拟模式时我无法接收任何位置,通常我每3秒接收一次位置并更新位置 - 当模拟模式被禁用时 - 但是使用模拟模式启用我无法获得任何位置

@Override
public void onConnected(Bundle bundle) {
    Utils.printDebug("onConnected");

    Location mockLocation = new Location("network");
    mockLocation.setLatitude(50.120089);
    mockLocation.setLongitude(18.993206);
    mockLocation.setAccuracy(4.0f);
    mockLocation.setTime(System.currentTimeMillis());
    LocationServices.FusedLocationApi.setMockMode(mGoogleApiClient, true);
    LocationServices.FusedLocationApi.setMockLocation(mGoogleApiClient, mockLocation);

    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);

    if(mLastLocation!=null)
        Utils.showToast(getApplicationContext(), "Last know location is " + mLastLocation.getLatitude() + ", " + mLastLocation.getLongitude());

    startLocationUpdates();
}
Run Code Online (Sandbox Code Playgroud)

我有<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />和开发人员选项模拟位置启用.

我怎样才能收到那个模拟位置?

hug*_*m84 6

首先,为了能够设置和使用模拟位置,您需要有一个调试清单:

SRC /调试/ AndroidManifest.xml中

具有以下权限:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ALLOW_MOCK_LOCATIONS"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
Run Code Online (Sandbox Code Playgroud)

并在调试模式下运行您的应用程序.

其次,您需要启用Settings.Secure.ALLOW_MOCK_LOCATION.在设备上执行此操作,设置 - >开发人员设置 - >允许模拟位置

然后你有两个选择,让你的Activity实现

import com.google.android.gms.location.LocationListener
Run Code Online (Sandbox Code Playgroud)

或者使用

PendingIntent 
Run Code Online (Sandbox Code Playgroud)

作为位置处理程序.

假设你实施

 GoogleApiClient.ConnectionCallbacks,
 GoogleApiClient.OnConnectionFailedListener
Run Code Online (Sandbox Code Playgroud)

连接到onConnected(Bundle bundle)回调中的LocationServices

使用LocationListener的示例:

@Override
public void onConnected(Bundle bundle) {

    LocationServices.FusedLocationApi.setMockMode(mGoogleClient, true);
    LocationRequest locationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
            .setFastestInterval(5000L)
            .setInterval(10000L);

    LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleClient, locationRequest, this);

@Override
public void onLocationChanged(Location location) {
    Log.d(TAG, "Location changed!" + location.toString());
}
Run Code Online (Sandbox Code Playgroud)

如果要使用PendingIntent,请将"this"替换为intent:*

    PendingIntent pendingIntent = PendingIntent.getService(this, 0,
            new Intent(this, LocationHandler.class),
            PendingIntent.FLAG_UPDATE_CURRENT);

    LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleClient, locationRequest, pendingIntent);
Run Code Online (Sandbox Code Playgroud)

LocationHandler扩展了IntentService:

@Override
protected void onHandleIntent(Intent intent) {
    final Location location = intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED);
    Log.d(TAG, "Got Location: " + location);

    Intent broadcastIntent = new Intent();
    broadcastIntent.setAction(TAG);
    broadcastIntent.putExtra(LOC_PARCEL, location);
    sendBroadcast(broadcastIntent);
}
Run Code Online (Sandbox Code Playgroud)

设置模拟位置

现在,您可以设置一个模拟位置:

    Location location = new Location("MockedLocation");
    // Time is needed to create a valid Location
    long currentTime = System.currentTimeMillis();
    long elapsedTimeNanos = SystemClock.elapsedRealtimeNanos();
    location.setElapsedRealtimeNanos(elapsedTimeNanos);
    location.setTime(currentTime);
    location.setLatitude(latitude);
    location.setLongitude(longitude);
    LocationServices.FusedLocationApi.setMockLocation(mGoogleClient, location);
Run Code Online (Sandbox Code Playgroud)

以下是如何实现正在移动的模拟位置:

final Handler handler = new Handler();

    final Runnable r = new Runnable() {
        public void run() {
            Location loc = route.getNextLocation();
            if(loc != null) {
                LocationServices.FusedLocationApi.setMockLocation(mGoogleClient, location);
                handler.postDelayed(this, 5000);
            }
        }
    };

    handler.postDelayed(r, 5000);
Run Code Online (Sandbox Code Playgroud)