domaven / xamarin-plugins

Cross platform Xamarin & Windows plugins for PCLs
MIT License
37 stars 51 forks source link

Plugin not initialized when app in background #30

Open pburgio opened 7 years ago

pburgio commented 7 years ago

When app is not active, you get a GeofenceNotInitializedException exception (see stack below all)

This is because at GeofenbceBootReceiver::OnReceive (line 27) there is

        var geofenceInstance= CrossGeofence.Current;

that cause the getter to throw the exception.I currently put a try-catch, is there a better workaround? In my opinion, that line of code is unnecessary (please, correct me if I'm wrong).

Ciao! Paolo

--------- beginning of crash 07-13 13:57:43.748 10906 10906 E AndroidRuntime: FATAL EXCEPTION: main 07-13 13:57:43.748 10906 10906 E AndroidRuntime: Process: XXXXXXX, PID: 10906 07-13 13:57:43.748 10906 10906 E AndroidRuntime: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException 07-13 13:57:43.748 10906 10906 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 07-13 13:57:43.748 10906 10906 E AndroidRuntime: Caused by: java.lang.reflect.InvocationTargetException 07-13 13:57:43.748 10906 10906 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 07-13 13:57:43.748 10906 10906 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 07-13 13:57:43.748 10906 10906 E AndroidRuntime: ... 1 more 07-13 13:57:43.748 10906 10906 E AndroidRuntime: Caused by: android.runtime.JavaProxyThrowable: Geofence.Plugin.Abstractions.GeofenceNotInitializedException: CrossGeofence - Plugin is not initialized. Should initialize before use with CrossGeofence Initialize method. Example: CrossGeofence.Initialize() 07-13 13:57:43.748 10906 10906 E AndroidRuntime: at Geofence.Plugin.CrossGeofence.get_Current () [0x00013] in <0f602bb09ad94d9996a5adee15e35701>:0 07-13 13:57:43.748 10906 10906 E AndroidRuntime: at Geofence.Plugin.GeofenceBootReceiver.OnReceive (Android.Content.Context context, Android.Content.Intent intent) [0x0001b] in <0f602bb09ad94d9996a5adee15e35701>:0 07-13 13:57:43.748 10906 10906 E AndroidRuntime: at Android.Content.BroadcastReceiver.n_OnReceive_Landroid_content_Context_Landroid_contentIntent (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_context, System.IntPtr

pburgio commented 7 years ago

PS the issue disappears e.g., in my huawei + nougat device

pmdauv commented 7 years ago

Hi Paolo, Will you tell us how you solved the issue: Android project does not compile when using Geofence.Plugin with latest GooglePlayServices. I have the same issue unfortunately no solution to solve it.

Thank you for help, Placide.

pburgio commented 7 years ago

Hello,

I simply surrounded with a try-catch this line of code -- GeofenbceBootReceiver::OnReceive (line 27): var geofenceInstance= CrossGeofence.Current;

As said, in my huawai p10 w/nougtat, I don't experience the issue anymore..even without try-catch-ing that line

Ciao! Paolo

Westat-Transportation commented 5 years ago

We addressed this by adding waits to the geofence handlers that only go through after the geofence plugin is initialized, we track initialization in our geofence handling class using a boolean that is set once OnAppInitialized is called.

Westat-Transportation commented 5 years ago

We submitted a couple pull requests that address this and the authors of this package released a new plugin that incorporate them. Try https://www.nuget.org/packages/Plugin.Geofence/, it is almost a complete drop in replacement with the exception of a namespace change and can be used in projects targeting API 27.