gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:16:49 25 4
gpt4 key购买 nike

当用户进入和退出地理围栏时,我的应用程序使用地理围栏 服务 向 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();
}
}
}

主 Activity .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>

来 self 的 list 。

关于c# - Xamarin Android - BroadcastReceiver 问题 - 无法实例化接收器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31998005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com