Xamarin Android - BroadcastReceiver问题-无法实例化receiver

本文关键字:实例化 receiver 问题 Android BroadcastReceiver Xamarin | 更新日期: 2023-09-27 18:07:46

我的应用程序正在使用Geofencing Service在用户进入和退出Geofence时向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]]

有趣的是,这只发生在位置服务更改时,而不是在飞行模式更改时。在这两种情况下,OnReceive在我的BroadcastReceiver被调用。

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

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

Xamarin Android - BroadcastReceiver问题-无法实例化receiver

啊,我明白了。

我必须去掉

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