Dev-hwang / flutter_foreground_task

This plugin is used to implement a foreground service on the Android platform.
https://pub.dev/packages/flutter_foreground_task
MIT License
152 stars 112 forks source link

Error turn off screen when app is active Android 14 #273

Closed tuyen3962 closed 2 months ago

tuyen3962 commented 2 months ago

library version:

Flutter 3.22.3 flutter_foreground_task: ^8.8.0

Description issue: When I move the application to the background, it work fine. But when the application is active in the screen, I turn off my device and it throw error

java.lang.RuntimeException: Unable to start service com.pravera.flutter_foreground_task.service.ForegroundService@3cbbdfd with Intent { cmp=com.vnconnections.app/com.pravera.flutter_foreground_task.service.ForegroundService }: java.lang.SecurityException: Starting FGS with type location callerApp=ProcessRecord{f3168 30672:com.vnconnections.app/u0a496} targetSDK=34 requires permissions: all of the permissions allOf=true [android.permission.FOREGROUND_SERVICE_LOCATION] any of the permissions allOf=false [android.permission.ACCESS_COARSE_LOCATION, android.permission.ACCESS_FINE_LOCATION] and the app must be in the eligible state/exemptions to access the foreground only permission

Caused by: java.lang.SecurityException: Starting FGS with type location callerApp=ProcessRecord{f3168 30672:com.vnconnections.app/u0a496} targetSDK=34 requires permissions: all of the permissions allOf=true [android.permission.FOREGROUND_SERVICE_LOCATION] any of the permissions allOf=false [android.permission.ACCESS_COARSE_LOCATION, android.permission.ACCESS_FINE_LOCATION] and the app must be in the eligible state/exemptions to access the foreground only permission

Please support me in this case, I also setup the manifest as you mention in other issue and the document but it doesn't work for me. Please revise my manifest

Dev-hwang commented 2 months ago

According to the error, location permissions must be declared and requested.

Add the following to Manifest file:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

Request location permission using permission_handler.

You must meet runtime prerequisites before starting the service. https://developer.android.com/about/versions/14/changes/fgs-types-required#location

It's explained in the readme.

tuyen3962 commented 2 months ago

I also add these following permission in Manifest file. But it does not work. It only occur in the case of turning off the screen when the application is active

tuyen3962 commented 2 months ago

With the case of integrating the isar and timer in the foreground task, do i need to add the permission in the manifest?

Dev-hwang commented 2 months ago

i have no experience with isar. can you provide sample project?

tuyen3962 commented 2 months ago

Oke wait me a minutes. But I test with the always location permission and it work for me when turn off the screen. With the case of whileInUse it dont' work. However, I test in other with just WhileInUse permission and it work in the case of turning off screen when app is active in the screen and put the application into background mode.

Dev-hwang commented 2 months ago

You must always allow the Location permission to get your location in the background.

tuyen3962 commented 2 months ago

But in the other application, it work with the while in use location permission when it run in the background

Dev-hwang commented 2 months ago

ok. sample project is ready, I check this issue.

tuyen3962 commented 2 months ago

So do I need give you the sample project to test this case?

Dev-hwang commented 2 months ago

I tested in whileInUse state but the above error did not occur 😢

I would appreciate it if you could provide a sample project.

tuyen3962 commented 2 months ago

Oke, you can try to run in during the amount of time like 30m to whether it run properly

tuyen3962 commented 2 months ago

https://github.com/tuyen3962/location_example.git

I have created the example basing on my app. You can check it. But in this example, it don't work with the case of while in use location. But I face another issue that I crash when call this function openIgnoreBatteryOptimizationSettings

Dev-hwang commented 2 months ago

@tuyen3962

I found it. This is related to limitations on Android platform.

The android official documentation explains it as follows:

For while-in-use permissions, this causes a potential problem. If your app has a while-in-use permission, it only has that permission while it's in the foreground. This means if your app is in the background, and it tries to create a foreground service of type camera, location, or microphone, the system sees that your app doesn't currently have the required permissions, and it throws a SecurityException.

You are trying to start the location service when AppLifecycleState.hidden:

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (!hasStart && state == AppLifecycleState.hidden) {
      hasStart = true;
      LocationBackgroundSetup.startService(); // here
    } else if (hasStart && state == AppLifecycleState.resumed) {
      hasStart = false;
      LocationBackgroundSetup.stopService();
    }
    super.didChangeAppLifecycleState(state);
  }

The moment you turn off the screen, AppLifecycleState.hidden will be triggered and the app will enter the background state.

At this time, if you try to start location service in the while-in-use state, a SecurityException will be thrown.

This is a platform limitation, so you should always allow location permission 😢

tuyen3962 commented 2 months ago

ak this demo I start the service when the state of app is hidden. But in my personal project, I check it when the application is inactive and it work. Is there any trick? :)))

Dev-hwang commented 2 months ago

In my case, SecurityException occurs even in AppLifecycleState.active state.

I think you should always allow permission to safely start the service.

tuyen3962 commented 2 months ago

Huh, maybe that. Thank you for your support