Android Question Mock Location

Lucas Eduardo

Active Member
Licensed User
Hello,
I was trying to do this service mock location from this post https://www.b4x.com/android/forum/threads/mock-locationprovider-lib.14266/, but the example don't work.

I'm getting this error
B4X:
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (mlpservice) Create ***
** Service (mlpservice) Start **
Error occurred on line: 110 (MLPservice)
java.lang.IllegalArgumentException: Incomplete location object, missing timestamp or accuracy? Location[gps 52,569035,13,196660 hAcc=2 et=?!? alt=84.0 vel=0.0 bear=75.0 vAcc=??? sAcc=??? bAcc=???]
    at android.location.LocationManager.setTestProviderLocation(LocationManager.java:1279)
    at esolutions4you.B4A.moclocationprovider.moclocationprovider.publishMockLocation(moclocationprovider.java:153)
    at de.esolutions4you.mlpdemo.mlpservice._my_timer_tick(mlpservice.java:177)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.objects.Timer$TickTack.run(Timer.java:105)
    at android.os.Handler.handleCallback(Handler.java:794)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:176)
    at android.app.ActivityThread.main(ActivityThread.java:6662)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
** Activity (main) Pause, UserClosed = true **
** Service (mlpservice) Destroy **

I searched on internet and found this https://stackoverflow.com/questions...not-being-set-despite-all-parameters/30381074

How can i fix the problem?

My manifest
B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="28"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.DarkTheme)
'End of default text.
AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddPermission(android.permission.ACCESS_COARSE_LOCATION)
AddPermission(android.permission.ACCESS_MOCK_LOCATION)

I tried with older sdkVersion, but also did not work.
I also already checked in developer option the app of mock location

Thanks.
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
How can i fix the problem?
only if you have the source of the lib. i guess. I tried a code i found on SO
Location loc = new Location(mocLocationProvider);
Location mockLocation = new Location(mocLocationProvider); // a string
mockLocation.setLatitude(-26.902038); // double
mockLocation.setLongitude(-48.671337);
mockLocation.setAltitude(loc.getAltitude());
mockLocation.setTime(System.currentTimeMillis());
lm.setTestProviderLocation( mocLocationProvider, mockLocation);
It results in the same error.

After changing it to
B4X:
    Location loc = new Location(mocLocationProvider);
    Location mockLocation = new Location(mocLocationProvider); // a string
    mockLocation.setLatitude(-26.902038);  // double
    mockLocation.setLongitude(-48.671337);
    mockLocation.setAltitude(loc.getAltitude());
    mockLocation.setTime(System.currentTimeMillis());
    mockLocation.setAccuracy(1);
    mockLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
    lm.setTestProviderLocation( mocLocationProvider, mockLocation);

it no longer crashes.
Though this is only a quit and dirty test. It is not really a library and it is not using any services.... Nothing i can share at this time.
 
Upvote 0
Top