启动Xamarin前景服务样本崩溃

发布于 2025-02-09 17:13:15 字数 1302 浏览 1 评论 0原文

我正在寻找创建Xamarin前景服务,并认为从 https://github.com/xamarin/monodroid-samples/tree/main/main/applicationfundamentals/servicesampleass/foregroundservedemo 将是一个很好的第一步。 构建和部署到我的三星S10后,单击“开始服务”时,它只是崩溃了。

它失败的行是timestampservice.cs第155行,在代码上:

var pendingIntent = PendingIntent.GetActivity(this, 0, notificationIntent, PendingIntentFlags.UpdateCurrent); 

null引用异常。堆栈跟踪:

at Android.Runtime.JNINativeWrapper._unhandled_exception (System.Exception e) [0x0000e] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12 
at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLII_I (_JniMarshal_PPLII_I callback, System.IntPtr jnienv, System.IntPtr klazz, System.IntPtr p0, System.Int32 p1, System.Int32 p2) [0x00022] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:221 at (wrapper native-to-managed) Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLII_I(intptr,intptr,intptr,int,int)

我已经尝试了一些事情,包括更新到参考文献的最新版本,但我仍然没有解决此问题。 我尝试从头开始创建新的背景服务,但仍然会遇到相同的错误。

有人有什么想法,我如何解决这个问题?

I'm looking to create a Xamarin foreground service and thought taking the sample from https://github.com/xamarin/monodroid-samples/tree/main/ApplicationFundamentals/ServiceSamples/ForegroundServiceDemo would be a good first step.
After building and deploying to my Samsung S10 it just crashes when clicking 'Start Service'.

The line it fails on is TimeStampService.cs line 155, on the code:

var pendingIntent = PendingIntent.GetActivity(this, 0, notificationIntent, PendingIntentFlags.UpdateCurrent); 

With null reference exception. The stack trace:

at Android.Runtime.JNINativeWrapper._unhandled_exception (System.Exception e) [0x0000e] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12 
at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLII_I (_JniMarshal_PPLII_I callback, System.IntPtr jnienv, System.IntPtr klazz, System.IntPtr p0, System.Int32 p1, System.Int32 p2) [0x00022] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:221 at (wrapper native-to-managed) Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLII_I(intptr,intptr,intptr,int,int)

I've tried a few things, including updating to latest versions of references, but still nothing I do fixes this.
I tried creating a new background service from scratch and still get the same error.

Does anyone have any ideas how I can fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你在看孤独的风景 2025-02-16 17:13:15

您是否在AndroidManifest.xml中添加了前景服务的权限?

例如:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

更新
您可以尝试在我的旧答案中使用代码:在关闭应用程序关闭应用程序(使用AlarmManager)时在Android中发射本地通知的麻烦

此外,在MainAttivity中启动它:

 Intent intent = new Intent(this, typeof(ForgroundServiceDemo));
 StartForegroundService(intent);

Did you add the foreground service permission in the AndroidManifest.xml?

Such as:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

Update
You can try to use the code in my old answer:Trouble getting local notifications to fire in Android when the application is closed (using AlarmManager)

In addition, start it in the mainactivity:

 Intent intent = new Intent(this, typeof(ForgroundServiceDemo));
 StartForegroundService(intent);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文