在FusedLocation中接收模拟位置

时间:2015-02-04 09:20:05

标签: 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();
}

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

我如何才能收到该模拟位置?

1 个答案:

答案 0 :(得分:5)

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

  

的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"/>

并以调试模式运行应用程序。

其次,您需要启用Settings.Secure.ALLOW_MOCK_LOCATION。在设备上执行此操作,设置 - &gt;开发者设置 - &gt;允许模拟位置

然后你有两个选择,要么让你的活动实施

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

或使用

PendingIntent 

作为位置处理程序。

假设你实施

 GoogleApiClient.ConnectionCallbacks,
 GoogleApiClient.OnConnectionFailedListener

连接到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());
}

如果您想使用PendingIntent,请替换&#34;此&#34;意图:*

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

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

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);
}

设置模拟位置

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

    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);

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

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);