mauron85/cordova-plugin-background-geolocation

App crashes on android 10 when use ACTIVITY_PROVIDER

Opened this issue ยท 16 comments

Your Environment

  • Plugin version: 3.1.0
  • Platform: Android
  • OS version: 10
  • Device manufacturer and model: Honor 10 Lite.

Context

When call BackgroundGeolocation.start() app crashes

Expected Behavior

Start background geolocation.

Actual Behavior

App crashes

Possible Fix

https://developer.android.com/about/versions/10/privacy/changes#physical-activity-recognition

Steps to Reproduce

  1. Configuru background location with ACTIVITY_PROVIDER:
    BackgroundGeolocation.configure({locationProvider: BackgroundGeolocation.ACTIVITY_PROVIDER})
  2. Call BackgroundGeolocation.start()

Debug logs

08-18 11:28:22.384 6724 6724 E AndroidRuntime: FATAL EXCEPTION: main 08-18 11:28:22.384 6724 6724 E AndroidRuntime: Process: ru.okdesk.mobile, PID: 6724 08-18 11:28:22.384 6724 6724 E AndroidRuntime: java.lang.SecurityException: Activity detection usage requires the ACTIVITY_RECOGNITION permission 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at android.os.Parcel.createException(Parcel.java:2071) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at android.os.Parcel.readException(Parcel.java:2039) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at android.os.Parcel.readException(Parcel.java:1987) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at com.google.android.gms.internal.zzed.zzb(Unknown Source:10) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at com.google.android.gms.internal.zzccz.zza(Unknown Source:15) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at com.google.android.gms.internal.zzcdi.zza(Unknown Source:26) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at com.google.android.gms.internal.zzcbt.zza(Unknown Source:6) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at com.google.android.gms.internal.zzbax.zzb(Unknown Source:0) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at com.google.android.gms.internal.zzbbz.zze(Unknown Source:69) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at com.google.android.gms.internal.zzbcw.zze(Unknown Source:5) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at com.google.android.gms.internal.zzbco.zze(Unknown Source:152) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at com.google.android.gms.internal.zzcbs.requestActivityUpdates(Unknown Source:9) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at com.marianhello.bgloc.provider.ActivityRecognitionLocationProvider.attachRecorder(ActivityRecognitionLocationProvider.java:151) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at com.marianhello.bgloc.provider.ActivityRecognitionLocationProvider.onConnected(ActivityRecognitionLocationProvider.java:175) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at com.google.android.gms.common.internal.zzac.zzn(Unknown Source:109) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at com.google.android.gms.internal.zzbco.zzm(Unknown Source:22) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at com.google.android.gms.internal.zzbcc.zzpY(Unknown Source:89) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at com.google.android.gms.internal.zzbcc.onConnected(Unknown Source:21) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at com.google.android.gms.internal.zzbcw.onConnected(Unknown Source:7) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at com.google.android.gms.internal.zzbbh.onConnected(Unknown Source:5) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at com.google.android.gms.common.internal.zzaa.onConnected(Unknown Source:2) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at com.google.android.gms.common.internal.zzn.zzrj(Unknown Source:152) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at com.google.android.gms.common.internal.zze.zzs(Unknown Source:48) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at com.google.android.gms.common.internal.zzi.zzrk(Unknown Source:57) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at com.google.android.gms.common.internal.zzh.handleMessage(Unknown Source:275) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:107) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at android.os.Looper.loop(Looper.java:213) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:8178) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) 08-18 11:28:22.384 6724 6724 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: FATAL EXCEPTION: mainjava.lang.SecurityException: Activity detection usage requires the ACTIVITY_RECOGNITION permission 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at android.os.Parcel.createException(Parcel.java:2071) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at android.os.Parcel.readException(Parcel.java:2039) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at android.os.Parcel.readException(Parcel.java:1987) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at com.google.android.gms.internal.zzed.zzb(Unknown Source:10) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at com.google.android.gms.internal.zzccz.zza(Unknown Source:15) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at com.google.android.gms.internal.zzcdi.zza(Unknown Source:26) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at com.google.android.gms.internal.zzcbt.zza(Unknown Source:6) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at com.google.android.gms.internal.zzbax.zzb(Unknown Source:0) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at com.google.android.gms.internal.zzbbz.zze(Unknown Source:69) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at com.google.android.gms.internal.zzbcw.zze(Unknown Source:5) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at com.google.android.gms.internal.zzbco.zze(Unknown Source:152) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at com.google.android.gms.internal.zzcbs.requestActivityUpdates(Unknown Source:9) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at com.marianhello.bgloc.provider.ActivityRecognitionLocationProvider.attachRecorder(ActivityRecognitionLocationProvider.java:151) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at com.marianhello.bgloc.provider.ActivityRecognitionLocationProvider.onConnected(ActivityRecognitionLocationProvider.java:175) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at com.google.android.gms.common.internal.zzac.zzn(Unknown Source:109) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at com.google.android.gms.internal.zzbco.zzm(Unknown Source:22) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at com.google.android.gms.internal.zzbcc.zzpY(Unknown Source:89) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at com.google.android.gms.internal.zzbcc.onConnected(Unknown Source:21) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at com.google.android.gms.internal.zzbcw.onConnected(Unknown Source:7) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at com.google.android.gms.internal.zzbbh.onConnected(Unknown Source:5) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at com.google.android.gms.common.internal.zzaa.onConnected(Unknown Source:2) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at com.google.android.gms.common.internal.zzn.zzrj(Unknown Source:152) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at com.google.android.gms.common.internal.zze.zzs(Unknown Source:48) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at com.google.android.gms.common.internal.zzi.zzrk(Unknown Source:57) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at com.google.android.gms.common.internal.zzh.handleMessage(Unknown Source:275) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at android.os.Handler.dispatchMessage(Handler.java:107) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at android.os.Looper.loop(Looper.java:213) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at android.app.ActivityThread.main(ActivityThread.java:8178) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at java.lang.reflect.Method.invoke(Native Method) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) 08-18 11:28:22.387 6724 6724 E com.marianhello.logging.UncaughtExceptionLogger: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)

Hi, Android 10 requires an ACTIVITY_RECOGNITION permission to be asked explicitly. The fix is to update the permissions in BackgroundGeolocationFacade & LocationManager from

public static final String[] PERMISSIONS = {
        Manifest.permission.ACCESS_COARSE_LOCATION,
        Manifest.permission.ACCESS_FINE_LOCATION
};

to

    public static final String[] PERMISSIONS = {
           Manifest.permission.ACCESS_COARSE_LOCATION,
           Manifest.permission.ACCESS_FINE_LOCATION,
           Manifest.permission.ACTIVITY_RECOGNITION
   };

also make sure it exists in AndroidManifest.xml under uses-permission

@mauron85 I tried to add branch with fix, but seems it's prohibited. Can you review above changes? Fixed on my devices. Thanks

Note above will work for android >= 10, for android < 10 the permissions ACTIVITY_RECOGNITION should not be added

Hi @7h4r05, I'm trying to deal with this problem and I can't resolve it. Could you create a fork with this fix please? Thank you very much.

@bjdose hey man, sorry for delay. I created a fork here https://github.com/7h4r05/cordova-plugin-background-geolocation. Please let me know if it works for you.

I'm stucked on this for months. Trying to update my app to work with android X. If I manually grant the permission ACTIVITY_RECOGNITION (which is Phisycal Activity permission on the Android app) the plugin works great. Any fix for this?

Hi @jgauna ,
Did you fix that issue ??

I have this error. This plugin is supporting APIs 29 o Higher? @7h4r05 adding the permission solvent this error?

Thanks @biote-ven ,
I will try that. but i have one doubt, both in same plugins, which part is change in plugin can you please highlight that things,

@biote-ven yes, the Activity_Recognition permission is required for API >= 29, but for below versions you have to skip asking for that permission.


if (Build.VERSION.SDK_INT >= 29){
             permissions.add(Manifest.permission.ACTIVITY_RECOGNITION);
}

@7h4r05 @mauron85

In config.xml add and update Manifest (android.permission.ACTIVITY_RECOGNITION) using config-file

In the java file (BackgroundGeolocationFacade & LocationManager) add:
public static final String[] PERMISSIONS = {
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACTIVITY_RECOGNITION
};

Can you indicate the place to put this sentences?
if (Build.VERSION.SDK_INT >= 29){
permissions.add(Manifest.permission.ACTIVITY_RECOGNITION);
}

Tx

rx2go commented

Okay, I made it works.. but BackgroundGeolocation.getCurrentLocation always returns the same value in background. It works well on foreground.. and it does not send any requests to url or syncUrl as well..

Worked after added to Manifest following 2 strings and manually added permision at app settings..
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" /> <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />

After I changed options it`s alived:
stationaryRadius: 0,
distanceFilter: 0,

In low or equal API 28 working fine. But since API 29, it doesn't work. Sad!

@biote-ven yes, the Activity_Recognition permission is required for API >= 29, but for below versions you have to skip asking for that permission.


if (Build.VERSION.SDK_INT >= 29){
             permissions.add(Manifest.permission.ACTIVITY_RECOGNITION);
}

Apply this in TS file y the crash not ocurred... but the app is unestable. Not take coordinates! Next login is crash! Researching another alternatives.... have some more action i can make with this plugin?

@7h4r05 does your fork working fine at 19 september ?
I mean, does the app a wake up when the user is walking/driving ?

@dabaaaz I haven't tested it like this. I use it for tracking when user trigger it first.
On a side note don't forget to update AndroidManifest with

not only

I had this problem. And I managed to solve it like this: installed the plugin https://github.com/NeoLSN/cordova-plugin-android-permissions#readme and make a request to use ACTIVITY_RECOGNITION.

let permissions = cordova.plugins.permissions permissions.requestPermission(permissions.ACTIVITY_RECOGNITION, function (status) { console.log('[Log]', status) }, function (error) { console.log('[Error]', error) })

This causes an additional request from the user and there is no error

You need request ACTIVITY_RECOGNITION. This worked for me:

dariosalvi78/cordova-plugin-health#178 (comment)