Search code examples
c#androidxamarinbroadcastreceiverclassnotfoundexception

Xamarin Android - BroadcastReceiver Issue - Unable to instantiate receiver


My application is using a Geofencing Service to send http requests to an API when a user enters and exits a Geofence. I need that Service to be stopped or started depending on if the user has GPS access or not. I will also need this to occur after the application has been completely closed as disabling location service will crash the background service.

The problem is that whenever I enable or disable Location Services the application crashes with this stack trace:

java.lang.RuntimeException: Unable to instantiate receiver com.MyCompany.MyApp.GeofenceBroadcastReceiver: java.lang.ClassNotFoundException: Didn't find class "com.MyCompany.MyApp.GeofenceBroadcastReceiver" on path: DexPathList[[zip file "/data/app/com.MyCompany.MyApp-1/base.apk"],nativeLibraryDirectories=[/data/app/com.MyCompany.MyApp-1/lib/arm, /vendor/lib, /system/lib]]

Interestingly, this only occurs when Location Services is changed, not when Airplane mode is changed. In either case OnReceive in my BroadcastReceiver is being called.

I did manually edit my AndroidManifest.xml file. Not sure if that might have something to do with it. According to this it might.

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.MyCompany.MyApp" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="19" />
    <application android:label="MyApp" android:icon="@drawable/Icon">
        <meta-data android:name="come.google.android.maps.v2.API_KEY" android:value="SomeKeyValueForGoogleAPI" />
        <receiver android:name=".GeofenceBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.ACTION_AIRPLANE_MODE_CHANGED"></action>
                <action android:name="android.intent.action.ACTION_BOOT_COMPLETED"></action>
                <action android:name="android.location.PROVIDERS_CHANGED" />
            </intent-filter>
        </receiver>
    </application>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.LOCATION_HARDWARE" />
    <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_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.ACCESS_GPS" />
</manifest>

GeofenceBroadcastReceiver.cs

// using blah blah blah
namespace MyApp.Geofence
{
    [BroadcastReceiver]
    [IntentFilter(new[] { Intent.ActionBootCompleted, Intent.ActionAirplaneModeChanged, LocationManager.ProvidersChangedAction })]
    public class GeofenceBroadcastReceiver : BroadcastReceiver
    {
        public GeofenceBroadcastReceiver()
        {
        }

        public override void OnReceive(Context context, Intent intent)
        {
            LocationManager leManager = (LocationManager)context.GetSystemService(Context.LocationService);

            bool gpsEnabled = leManager.IsProviderEnabled(LocationManager.GpsProvider);
            bool networkEnabled = leManager.IsProviderEnabled(LocationManager.NetworkProvider);

            if (gpsEnabled)
            {
                // bool from the application preferences
                if (Preferences.getBool(Preferences.GEOLOCATION))
                    GeofenceApp.startService();
            }
            else
                GeofenceApp.stopService();
        }
    }
}

MainActivity.cs

// things...
private GeofenceBroadcastReceiver receiver;

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    //...
    // some other stuff
    //...
    receiver = new GeofenceBroadcastReceiver();
    RegisterReceiver(receiver, new IntentFilter(Intent.ActionBootCompleted));
    RegisterReceiver(receiver, new IntentFilter(LocationManager.ProvidersChangedAction));
    RegisterReceiver(receiver, new IntentFilter(Intent.ActionAirplaneModeChanged));
}

Solution

  • Gah, I figured it out.

    I had to actually remove

        <receiver android:name=".GeofenceBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.ACTION_AIRPLANE_MODE_CHANGED"></action>
                <action android:name="android.intent.action.ACTION_BOOT_COMPLETED"></action>
                <action android:name="android.location.PROVIDERS_CHANGED" />
            </intent-filter>
        </receiver>
    

    from my manifest.