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

时间:2015-08-13 20:39:16

标签: c# android xamarin broadcastreceiver classnotfoundexception

我的应用程序使用Geofencing Service在用户进入和退出Geofence时向API发送http请求。我需要停止或启动Service,具体取决于用户是否具有GPS访问权限。在应用程序完全关闭后,我还需要这样做,因为禁用位置服务会使后台服务崩溃。

问题在于,每当我启用或禁用位置服务时,应用程序都会崩溃:

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文件。不确定这可能与它有关。根据{{​​3}}它可能。

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

1 个答案:

答案 0 :(得分:7)

Gah,我明白了。

我必须实际删除

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

来自我的清单。