Xamarin Android - BroadcastReceiver 问题 - 无法实例化接收器

Xamarin Android - BroadcastReceiver Issue - Unable to instantiate receiver

我的应用程序使用地理围栏 Service 在用户进入和退出地理围栏时向 API 发送 http 请求。我需要根据用户是否具有 GPS 访问权限来停止或启动 Service。我还需要在应用程序完全关闭后发生这种情况,因为禁用定位服务会使后台服务崩溃。

问题是每当我启用或禁用位置服务时,应用程序都会崩溃并显示此堆栈跟踪:

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]]

有趣的是,这仅在更改定位服务时发生,而不是在更改飞行模式时发生。在任何一种情况下,我的 BroadcastReceiver 中的 OnReceive 都会被调用。

我确实手动编辑了我的 AndroidManifest.xml 文件。不确定这是否与它有关。根据 this 可能。

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));
}

哎呀,我想通了。

我不得不删除

    <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>

来自我的清单。