Android Question Inline java to Detect Mock Location

ttsolution

Member
Licensed User
Longtime User
Code: Copy
Hi all,
I use java Inline code to check for Mock Location Enable or Not but receive the error below

B4A version: 6.30
Parsing code.  (0.00s)
Compiling code.  (0.02s)
Compiling layouts code.  (0.00s)
Organizing libraries.  (0.00s)
Generating R file.  (0.03s)
Compiling debugger engine code.  (0.65s)
Compiling generated Java code.  Error
B4A line: 75
End Sub
javac 1.7.0_79
src\b4a\example\main.java:412: error: cannot find symbol
public static boolean areThereMockPermissionApps(Context context) {
  ^
  symbol:  class Context
  location: class main


Thanks for any help
Jonh

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
    NativeMe.InitializeContext
    End If

   Dim s As Boolean= NativeMe.RunMethod("isMockLocationOn", Null)
     Msgbox(s,"")
End Sub

#If JAVA
public boolean isMockLocationOn(Context context) {
  if (Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION).equals("0"))
  return false;
  else
  return true;
}
#End If
 
Last edited:
i guess you need to use IMPORT to include the right java classes to use inline
 
Upvote 0
Thanks DonManfred but I do not know what lib need to be included and how

Start here: https://developer.android.com/reference/android/provider/Settings.Secure.html.
See the fully qualified name for the Settings.Secure import is android.provider.Settings.Secure?

So the first import you need to add is:

Code: Copy
#If JAVA
import android.provider.Settings.Secure;

public boolean isMockLocationOn(Context context) {
  if (Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION).equals("0"))
  return false;
  else
  return true;
}
#End If

Then search the android reference for any other classes: https://developer.android.com/reference/android/content/Context.html will give you the fully qualified name for the Context class etc.
Add the additonal import(s) and you should be good to go.
 
Upvote 0
I received another error below
main_activity_create (java line: 347)
java.lang.ClassCastException: b4a.example.main cannot be cast to java.lang.Object[]
at b4a.example.main._activity_create(main.java:347)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at b4a.example.main.afterFirstLayout(main.java:106)
at b4a.example.main.access$000(main.java:21)
at b4a.example.main$WaitForLayout.run(main.java:84)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7223)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)


Code: Copy
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
    NativeMe.InitializeContext
    End If
   Dim s As Boolean = NativeMe.RunMethod("isMockLocationOn",NativeMe)
     Msgbox(s,"") 'will print Hello World!
End Sub

#If JAVA
import    android.provider.Settings ;
import android.provider.Settings.Secure;
import android.content.Context;
import android.test.mock.MockContext;

public boolean isMockLocationOn(Context context) {
  if (Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION).equals("0"))
  return false;
  else
  return true;
}
#End If
 
Upvote 0
You're passing the wrong parameter type to your inline java method.
The method wants an instance of android.content.Context but you're passing an instance of your Main class.

Take a look at this code:
Code: Copy
Sub GetContext As JavaObject
   Return GetBA.GetField("context")
End Sub

Sub GetBA As JavaObject
  Dim jo As JavaObject
  Dim cls As String = Me
  cls = cls.SubString("class ".Length)
  jo.InitializeStatic(cls)
  Return jo.GetFieldJO("processBA")
End Sub

GetContext should return the instance of android.content.Context that you require:
Code: Copy
Dim s As Boolean = NativeMe.RunMethod("isMockLocationOn",GetContext)
 
Upvote 0
Back
Top