gpt4 book ai didi

android - 在 FusedLocation 中接收模拟位置

转载 作者:太空狗 更新时间:2023-10-29 16:15:42 25 4
gpt4 key购买 nike

我设置了模拟位置,但是当启用模拟模式时我无法接收到任何位置,通常我每 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" />和模拟位置启用的开发人员选项。

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

最佳答案

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

src/debug/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"/>

并在 Debug模式下运行您的应用程序。

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

那么你有两个选择,要么让你的Activity实现

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,请将“this”替换为 Intent :*

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

关于android - 在 FusedLocation 中接收模拟位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28317652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com