flutter_background_service icon indicating copy to clipboard operation
flutter_background_service copied to clipboard

Background other libraries

Open VisualMafia opened this issue 3 years ago • 11 comments

I want to check the latest sms by timer. I can do this manually or through a listener. Tried different libraries, with any error. As far as I understand, there is no such possibility?

Small sample:

void onStart() async {
  WidgetsFlutterBinding.ensureInitialized();
  final service = FlutterBackgroundService();

Timer.periodic(Duration(seconds: 5), (timer) async {
    SmsReceiver receiver = new SmsReceiver();
    receiver.onSmsReceived.listen((msg) => print(msg.body));
  });

Error:

Error handling 'checkPlatformOverride' custom request: method not available: ext.flutter.platformOverride
Error handling 'checkBrightnessOverride' custom request: method not available: ext.flutter.brightnessOverride
Error handling 'checkPlatformOverride' custom request: method not available: ext.flutter.platformOverride
Error handling 'checkBrightnessOverride' custom request: method not available: ext.flutter.brightnessOverride
2
Error handling 'checkIsWidgetCreationTracked' custom request: method not available: ext.flutter.inspector.isWidgetCreationTracked

E/EventChannel#plugins.babariviere.com/recvSMS(10573): Failed to open event stream
E/EventChannel#plugins.babariviere.com/recvSMS(10573): java.lang.NullPointerException: Attempt to invoke virtual method 'int android.app.Activity.checkSelfPermission(java.lang.String)' on a null object reference

VisualMafia avatar Apr 23 '21 16:04 VisualMafia

Hi @VisualMafia, background service don't provide Ui, you have to check permission from the Ui before start the service.

ekasetiawans avatar Apr 23 '21 21:04 ekasetiawans

Yes, I did that too, in the beginning I created SmsReceiver receiver = new SmsReceiver ();, I asked for native permissions and then turned on the service. But, interestingly, vibro works without problems on a timer, audio too, device_info. And all libraries with reading calls or SMS do not work. Maybe there will be more ideas on how you can read SMS or calls in the background?

VisualMafia avatar Apr 24 '21 11:04 VisualMafia

@VisualMafia I think, the plugin depends on an Activity (android native class), but background service is not an Activity, so we can’t execute anything depends on Activity in background service since background service is able to run without Activity.

ekasetiawans avatar Apr 24 '21 13:04 ekasetiawans

Yes, it probably is. Although, it is very strange why such libraries have Activity, they do not have a UI. Don't you know if it is possible to run an application from a service if the application has been completely unloaded? Check the status by timer, if the OS is unloaded or the user, then start again.

VisualMafia avatar Apr 25 '21 19:04 VisualMafia

@VisualMafia because they need Activity for check and request permission.

ekasetiawans avatar Apr 25 '21 20:04 ekasetiawans

As far as I understand, even if I give all permissions in advance, all the same these libraries will use Activity and this cannot be solved?

VisualMafia avatar Apr 26 '21 07:04 VisualMafia

@VisualMafia yes, because the plugin source code need Activity's context to perform permission checks. as we can see, the plugin execute android.app.Activity.checkSelfPermission(java.lang.String).

ekasetiawans avatar Apr 26 '21 07:04 ekasetiawans

What would you do in this situation? :)

VisualMafia avatar Apr 26 '21 08:04 VisualMafia

@VisualMafia looking another plugin that doesn't require Activity

ekasetiawans avatar Apr 26 '21 08:04 ekasetiawans

Searched, could not find, probably because of permissions. Don't you know if you can run the application from the service if it has been unloaded?

VisualMafia avatar Apr 26 '21 09:04 VisualMafia

I will probably set a alarm to start app and check the state of the application through SystemChannels.lifecycle. Thanks for your answers and help!

VisualMafia avatar Apr 27 '21 09:04 VisualMafia