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

3
我的应用程序正在使用地理围栏服务,当用户进入和退出地理围栏时,发送http请求到API。我需要根据用户是否有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]]

有趣的是,只有在更改位置服务时才会发生这种情况,而更改飞行模式时不会发生。在任一情况下,我的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));
}
1个回答

7

噫,我想出来了。

实际上我需要将

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

从我的清单中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接