Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread #4

Open
zeexan-dev opened this issue Aug 5, 2019 · 15 comments

Comments

@zeexan-dev
Copy link

java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread

Future checkMockLocation() async { print("check mocklocation "); bool canMockLocation = await TrustFall.canMockLocation; print("mocklocation = "+ canMockLocation.toString()); }

@anish-adm
Copy link
Owner

@ENGR-ZEESHAN, Could you post output for flutter doctor please

@zeexan-dev
Copy link
Author

zeexan-dev commented Aug 5, 2019

@anish-adm
[√] Flutter (Channel stable, v1.7.8+hotfix.3, on Microsoft Windows [Version 10.0.18362.267], locale en-PK)

[√] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
[√] Android Studio (version 3.4)
[√] VS Code (version 1.36.1)
[!] Connected device

@mateusschmidt
Copy link

Same problem :/

E/TSLocationManager(22678): java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread. Current thread: Timer-1
E/TSLocationManager(22678): at io.flutter.embedding.engine.FlutterJNI.ensureRunningOnMainThread(FlutterJNI.java:807)
E/TSLocationManager(22678): at io.flutter.embedding.engine.FlutterJNI.invokePlatformMessageResponseCallback(FlutterJNI.java:740)
E/TSLocationManager(22678): at io.flutter.embedding.engine.dart.DartMessenger$Reply.reply(DartMessenger.java:156)
E/TSLocationManager(22678): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.success(MethodChannel.java:225)
E/TSLocationManager(22678): at com.anish.trust_fall.TrustFallPlugin$1.gotLocation(TrustFallPlugin.java:43)
E/TSLocationManager(22678): at com.anish.trust_fall.MockLocation.MockLocationCheck$GetLastLocation.run(MockLocationCheck.java:90)
E/TSLocationManager(22678): at java.util.TimerThread.mainLoop(Timer.java:562)
E/TSLocationManager(22678): at java.util.TimerThread.run(Timer.java:512)

@bagus-setyawan
Copy link

bagus-setyawan commented Oct 22, 2019

Same problem here, anyone get this resolve ?

Edited:
I found another alternative to detect just mock Location (another detection are working), with the same way this plugin do, install Geolocator flutter plugin then use this code :

Future<bool> checkMockLocation() async {
    Position position = await Geolocator().getLastKnownPosition();
    return position.mocked;
  }

@alifgiant
Copy link

@bagus-setyawan thanks a lot mate, this bug give me a headache.
Your code can bypass the bug

@shinsenter
Copy link

Same issue

E/AndroidRuntime( 6244): java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread. Current thread: Timer-1
E/AndroidRuntime( 6244):        at io.flutter.embedding.engine.FlutterJNI.ensureRunningOnMainThread(FlutterJNI.java:781)
E/AndroidRuntime( 6244):        at io.flutter.embedding.engine.FlutterJNI.invokePlatformMessageResponseCallback(FlutterJNI.java:718)
E/AndroidRuntime( 6244):        at io.flutter.embedding.engine.dart.DartMessenger$Reply.reply(DartMessenger.java:144)
E/AndroidRuntime( 6244):        at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.success(MethodChannel.java:231)
E/AndroidRuntime( 6244):        at com.anish.trust_fall.TrustFallPlugin$1.gotLocation(TrustFallPlugin.java:43)
E/AndroidRuntime( 6244):        at com.anish.trust_fall.MockLocation.MockLocationCheck$GetLastLocation.run(MockLocationCheck.java:88)
E/AndroidRuntime( 6244):        at java.util.TimerThread.processTask(Timer.java:577)
E/AndroidRuntime( 6244):        at java.util.TimerThread.mainLoop(Timer.java:532)
E/AndroidRuntime( 6244):        at java.util.TimerThread.run(Timer.java:512)
I/Process ( 6244): Sending signal. PID: 6244 SIG: 9

@Lzyct
Copy link

Lzyct commented Aug 25, 2020

@anish-adm could you check this Similiar issue, ?

i think this code make error

class GetLastLocation extends TimerTask {
        @Override
        public void run() {
            lm.removeUpdates(locationListenerGps);
            lm.removeUpdates(locationListenerNetwork);
            Location net_loc=null, gps_loc=null;
            if(gps_enabled)
                gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if(network_enabled)
                net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            //if there are both values use the latest one
            if(gps_loc!=null && net_loc!=null){
                if(gps_loc.getTime()>net_loc.getTime())
                    locationResult.gotLocation(gps_loc);
                else
                    locationResult.gotLocation(net_loc);
                return;
            }

            if(gps_loc!=null){
                locationResult.gotLocation(gps_loc);
                return;
            }
            if(net_loc!=null){
                locationResult.gotLocation(net_loc);
                return;
            }
            locationResult.gotLocation(null);
        }
    }

@iampopal
Copy link

I am also getting this error
Methods marked with @UiThread must be executed on the main thread. Current thread: Timer-0

when calling
bool canMockLocation = await TrustFall.canMockLocation;

@shinsenter
Copy link

Still crashed with the latest Flutter version:

2020-09-07 16:35:51.858 19394-19683/com.mycompany.testapp E/AndroidRuntime: FATAL EXCEPTION: Timer-0
    Process: com.mycompany.testapp, PID: 19394
    java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread. Current thread: Timer-0
        at io.flutter.embedding.engine.FlutterJNI.ensureRunningOnMainThread(FlutterJNI.java:992)
        at io.flutter.embedding.engine.FlutterJNI.invokePlatformMessageResponseCallback(FlutterJNI.java:779)
        at io.flutter.embedding.engine.dart.DartMessenger$Reply.reply(DartMessenger.java:144)
        at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.success(MethodChannel.java:235)
        at com.anish.trust_fall.TrustFallPlugin$1.gotLocation(TrustFallPlugin.java:43)
        at com.anish.trust_fall.MockLocation.MockLocationCheck$GetLastLocation.run(MockLocationCheck.java:88)
        at java.util.TimerThread.processTask(Timer.java:577)
        at java.util.TimerThread.mainLoop(Timer.java:532)
        at java.util.TimerThread.run(Timer.java:512)
2020-09-07 16:35:52.020 19394-19505/com.mycompany.testapp D/FA: Event not sent since app measurement is disabled
2020-09-07 16:35:52.069 19394-19683/com.mycompany.testapp I/Process: Sending signal. PID: 19394 SIG: 9

@sunnykinger
Copy link

sunnykinger commented Nov 30, 2020

Same problem here, anyone get this resolve ?

Edited:
I found another alternative to detect just mock Location (another detection are working), with the same way this plugin do, install Geolocator flutter plugin then use this code :

Future<bool> checkMockLocation() async {
    Position position = await Geolocator().getLastKnownPosition();
    return position.mocked;
  }

I think its now updated

Future<bool> checkMockLocation() async {
    Position position = await Geolocator.getLastKnownPosition();
    return position.isMocked;
  }

@sunnykinger
Copy link

Same problem here, anyone get this resolve ?

Edited:
I found another alternative to detect just mock Location (another detection are working), with the same way this plugin do, install Geolocator flutter plugin then use this code :

Future<bool> checkMockLocation() async {
    Position position = await Geolocator().getLastKnownPosition();
    return position.mocked;
  }

try to execute this code after turning off and turning on location. It gives some kind of Exception.

@ghost
Copy link

ghost commented Dec 29, 2020

Is there any update on solving this error. I am also facing the same issue. I am on the latest build of TrustFall and flutter.

@wisnuwiry
Copy link

Same problem here, after building App in release mode.

@bagus-setyawan
Copy link

@sunnykinger , @rp014947 , @wisnuwiry guys just use location plugin your own choice and make sure if permission permitted and get mocked properties from that, if you look into this project code you will notice that this is same way. Or you can make a fork and resolved the issue.

But once again users can pass this checker with many ways (rooted phone etc) so add another validations if your app consent is about real user's location.

@thanhit93
Copy link

thanhit93 commented Nov 22, 2021

Fix running on @UiThread :
@OverRide
public void onMethodCall(MethodCall call, @nonnull final Result result) {
if (call.method.equals("getPlatformVersion")) {
result.success("Android " + android.os.Build.VERSION.RELEASE);
} else if (call.method.equals("isJailBroken")) {
result.success(RootedCheck.isJailBroken(applicationContext));
} else if (call.method.equals("canMockLocation")) {
MockLocationCheck.LocationResult locationResult = new MockLocationCheck.LocationResult(){
@OverRide
public void gotLocation(final Location location){
android.os.Handler handler = new android.os.Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@OverRide
public void run() {
//Got the location!
if(location != null){
result.success(location.isFromMockProvider());
}else {
result.success(false);
}
}
});
}
};
MockLocationCheck mockLocationCheck = new MockLocationCheck();
mockLocationCheck.getLocation(applicationContext, locationResult);
}else if (call.method.equals("isRealDevice")) {
result.success(!EmulatorCheck.isEmulator());
}else if (call.method.equals("isOnExternalStorage")) {
result.success(ExternalStorageCheck.isOnExternalStorage(applicationContext));
}
else {
result.notImplemented();
}
}

Checking my github: thanhit93@8a24ca5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests