【问题标题】:Xamarin.Forms: Broadcast receiver not working when app is closedXamarin.Forms:应用程序关闭时广播接收器不工作
【发布时间】:2015-08-20 04:07:27
【问题描述】:

我正在使用 Xamarin.Forms 开发一个应用程序,虽然其中大部分工作正常,但我无法在应用程序不在 RAM 中时让我的广播接收器工作 - 这意味着即使当它是由安卓“轻轻”关闭的,而不是由用户强制关闭。

当应用程序在前台运行或应用程序在后台运行但尚未终止时,广播接收器本身可以正常工作。我花了几天时间寻找解决方案,但我无法成功。我在 Xamarin 论坛和此处看到了一些提示,用于在服务中声明我的广播接收器,并在 MainActivity 文件中启动该服务,虽然服务似乎正在运行,但它仍然没有对我的本地通知做任何事情。

据我了解,在 Java Android 开发中,您所要做的就是在清单文件中声明接收器,它会起作用,但在使用 Xamarin(否则很棒)技术时,情况似乎并非如此。

我希望你们中的一些人也遇到过这个问题并且知道解决方案,因为我非常绝望。非常感谢您的宝贵时间,祝您有愉快的一天!

编辑:

Receiver 是使用 Xamarin 的代码方式声明的,在生成的 Manifest 中是这样的:

receiver android:name="md5d0a2bd06806e04fc29264ca7dfdf52f5.AlarmService"

这是广播接收器本身:

[BroadcastReceiver(Enabled=true)]
public class AlarmService : BroadcastReceiver, IAlarmService
{
    List<LocalNotification> notifications;
    public override void OnReceive (Context context, Intent intent)
    {
        PowerManager pm = (PowerManager) context.GetSystemService(Context.PowerService);
        PowerManager.WakeLock wl = pm.NewWakeLock(WakeLockFlags.Partial, "");
        wl.Acquire();

        Intent resultIntent = new Intent(context, context.GetType());
        resultIntent.PutExtra ("Notification", true);

        IList<string> values = intent.GetStringArrayListExtra ("NotificationStrings");

        new LocalNotificationService(context).Notify (values [0], values [1], values [2], values[3]);
        wl.Release();       
    }

它接收到的 PendingIntent 是这样设置的:

Intent intent = new Intent (context, typeof(AlarmService));
        intent.SetAction (notifications [requestCode].Id ?? Guid.NewGuid ().ToString ());
        intent.PutStringArrayListExtra ("NotificationStrings", new List<string>() {
            notifications [requestCode].Title,
            notifications [requestCode].Body,
            notifications [requestCode].AlertAction,
            notifications [requestCode].UserData,
            notifications [requestCode].Id
        });
AlarmManager am = (AlarmManager)context.GetSystemService(Context.AlarmService);

        PendingIntent pi = PendingIntent.GetBroadcast(context, 0, intent, 0);

        am.Set (AlarmType.RtcWakeup, (long)(Java.Lang.JavaSystem.CurrentTimeMillis () + (dateTime.Value.ToUniversalTime () - DateTime.Now.ToUniversalTime ()).TotalMilliseconds), pi);

但是,即使我删除 OnReceive 中的代码,我也没有发生任何事情,而是收到系统消息,指出应用程序已停止工作,这让我认为上下文或 Intent 一定有问题 - 应用程序时一切正常活动在 RAM 中。

【问题讨论】:

  • 你把 Intent 放在哪里了?是在行动期间还是事件期间?

标签: c# android xamarin broadcastreceiver xamarin.forms


【解决方案1】:

您需要在应用清单中添加在后台运行的权限。 查看我的工作解决方案here

【讨论】:

  • 谢谢,我只使用本地通知,但我想我已经解决了部分问题。现在看来广播接收器确实启动了,但是在该应用程序崩溃之后。我只是将整个通知逻辑移动到简单的占位符项目中,我正在那里进行试验......我怀疑我的接收器的 Context 参数有问题,这也是一个同时用作警报设置器的类(或者这是错误的吗?)。我将用一些代码 sn-ps 编辑原始帖子,如果你能看一下。还是非常感谢!
【解决方案2】:

原来我是个彻头彻尾的白痴;问题不在接收器或警报设置器中。我在 LocalNotificationService 中处理了错误的上下文。我在整个时间内使用 Forms.Context 而不是接收器传递的 Context。

抱歉,各位,浪费了你们的时间。感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多