Skip to content

Class cast exception #7

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

Closed
zawadZwd opened this issue Jul 14, 2021 · 8 comments
Closed

Class cast exception #7

zawadZwd opened this issue Jul 14, 2021 · 8 comments

Comments

@zawadZwd
Copy link

zawadZwd commented Jul 14, 2021

Hello there,
tldr; when i'm trying to start foreground tast I get this exception:

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
	at com.pravera.flutter_foreground_task.service.ForegroundServiceManager.saveOptions(ForegroundServiceManager.kt:98)
	at com.pravera.flutter_foreground_task.service.ForegroundServiceManager.start(ForegroundServiceManager.kt:24)
	at com.pravera.flutter_foreground_task.MethodCallHandlerImpl.onMethodCall(MethodCallHandlerImpl.kt:47)
	at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
	at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
	at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:818)
	at android.os.MessageQueue.nativePollOnce(Native Method)
	at android.os.MessageQueue.next(MessageQueue.java:325)
	at android.os.Looper.loop(Looper.java:142)
	at android.app.ActivityThread.main(ActivityThread.java:6944)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

My code is pretty much like in the example and I have no clue where to find the problem. In a task, I'm displaying the timestamp so I do not know what kind of integer is trying to be cast into long.

@zawadZwd
Copy link
Author

zawadZwd commented Jul 14, 2021

EDIT. Forget about it, I didn't add service to AndroidManifest. But in the issue above, all code was done step by step with the documentation.

This is getting weirder and weirder. I took your example and installed it on the device with Android 8.0, when I clicked the start button, the console shows me only:
[log] 2021-07-14 11:35:00.822222 FlutterForegroundTask started.
and nothing else appears.

The same happened on Android 10 and 11.

@Dev-hwang
Copy link
Owner

@zawadZwd
Can you show me the callback() function you implemented?

@zawadZwd
Copy link
Author

zawadZwd commented Jul 14, 2021

Sure thing

void geolocatorCallback() async {
  try {
    FlutterForegroundTask.initDispatcher((timestamp) async {
      var localization = await Geolocator.getCurrentPosition();
      dev.log('timestamp: ${timestamp.toString().substring(0, 19)}');
      FlutterForegroundTask.update(
        notificationTitle: 'Current GPS localization',
        notificationText: '${localization.latitude}, ${localization.longitude}',
      );
    }, onDestroy: (timestamp) async {
      dev.log('geolocatorCallback() is dead.. x_x');
    });
  } catch (e) {
    dev.log('Geolocator callback error: $e');
  }
}

Even without localization variable the result is the same

@zawadZwd
Copy link
Author

zawadZwd commented Jul 14, 2021

But from my observation looks like the issue occurs after invoking task trigger:

Future<void> turnOnGps() async {
      try {
        var localization = await Geolocator.getCurrentPosition();
        FlutterForegroundTask.start(
          notificationTitle: 'Current GPS localization',
          notificationText: '${localization.latitude}, ${localization.longitude}',
          callback: geolocatorCallback,
        );
      } catch (e) {
        print('Error: $e');
      }
  }

@Dev-hwang
Copy link
Owner

@zawadZwd
yes. I'll test it in a little bit and let you know the results.

@Dev-hwang
Copy link
Owner

@zawadZwd
The first issue is a type casting error that can be fixed quickly. I will update it as soon as possible to prevent any bugs from occurring :)

The second issue is most likely a permission issue. Check this out.

  1. Make sure you have added background permission.
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
  1. Make sure the location permission is always allowed. I recently saw a problem with the Geolocator plugin that didn't work well for permission requests. requestPermission not working when "ACCESS_BACKGROUND_LOCATION" is given Baseflow/flutter-geolocator#771
    image

  2. This is the best way to do this if you are providing streams to the plugins you want to use.

void callback() {
  final positionStream = Geolocator.getPositionStream();
  StreamSubscription<Position>? streamSubscription;

  FlutterForegroundTask.initDispatcher((timestamp) async {
    if (streamSubscription != null) return;

    streamSubscription = positionStream.listen((event) {
      print('timestamp: ${timestamp.toString()}');

      FlutterForegroundTask.update(
          notificationTitle: 'Current GPS localization',
          notificationText: '${event.latitude}, ${event.longitude}');
    });
  }, onDestroy: (timestamp) async {
    await streamSubscription?.cancel();
    print('callback() is dead.. x_x');
  });
}

@Dev-hwang
Copy link
Owner

@zawadZwd
Fixed a type casting error. Upgrade to flutter_foreground_task: ^2.0.5.

@zawadZwd
Copy link
Author

You rock! Things work perfectly fine now :)
fun - and strange - fact is the previous version, ^2.0.4, worked well on M1 Macbook with the Android emulator, after cloning repo to windows, it stops. But now it doesn't even matter ;) time to close it. Thank you very very much!

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

2 participants