1

I have a need to create a test application which can mock the location. On clicking button, the latitude and longitude values provided in text box need to be set as the mock location. I am trying to build this using simple way using fused location provider APIs

Location mockLocation = new Location("flp");
mockLocation.setLatitude(12);
mockLocation.setLongitude(82);
mockLocation.setAccuracy(1.0f);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    mockLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
}
mockLocation.setTime(new Date().getTime());


Criteria criteria = new Criteria();
criteria.setAccuracy( Criteria.ACCURACY_FINE );
String provider = locationManager.getBestProvider( criteria, true );

if ( provider == null ) {
   Log.e( "", "No location provider found!" );
   return;
}

// provider and LocationManager.GPS_PROVIDER are same - "gps"

locationManager.setTestProviderLocation(LocationManager.GPS_PROVIDER, mockLocation); // --> I am getting exception here

I gave all possible permissions in manifest file:

ACCESS_COARSE_LOCATION
ACCESS_FINE_LOCATION
ALLOW_MOCK_LOCATIONS
ACCESS_MOCK_LOCATION

Also manually enabled developer options, application permissions, and mock location provider app in settings.

I am getting except on setTestProviderLocation:

java.lang.IllegalStateException: Could not execute method for android:onClick at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390) at android.view.View.performClick(View.java:6266) at android.view.View$PerformClick.run(View.java:24730) at android.os.Handler.handleCallback(Handler.java:793) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:173) at android.app.ActivityThread.main(ActivityThread.java:6698) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) ... 9 more Caused by: java.lang.IllegalArgumentException: Provider "gps" unknown at android.os.Parcel.readException(Parcel.java:1955) at android.os.Parcel.readException(Parcel.java:1897) at android.location.ILocationManager$Stub$Proxy.setTestProviderLocation(ILocationManager.java:1340) at android.location.LocationManager.setTestProviderLocation(LocationManager.java:1305)

Any way to fix this?

Thanks, raj

4

1 回答 1

0

由于Android 10我的假定位应用程序有同样的问题,应用程序崩溃:

java.lang.IllegalArgumentException: Provider "gps" unknown

但在较旧的 Android 版本上,相同的代码正在运行。我在 6、7、8 和 9 上对其进行了测试。我的临时解决方案是捕获异常(直到找到更好的方法)。模拟应该仍然有效。

try {
   mLocationManager.removeTestProvider(getBestProvider());
} catch (IllegalArgumentException e) {
   // handle the error e.getMessage()
}

在调试过程中,我可以在 locationManager 列表中看到提供程序 gps,例如

mLocationManager.getProvider(getBestProvider())
于 2019-09-28T19:01:06.310 回答