Xamarin - 警报触发但接收器未被调用
Xamarin - Alarm triggers but Receiver doesn't get called
所以我使用 AlarmManager 设置了一个警报,adb logcat 显示警报触发但它不会执行 Receiver 的 OnReceive 函数。
清单:
<receiver android:name=".AlarmReceiver" android:enabled="true">
<intent-filter>
<action android:name="com.companyname.APPNAME.SEND.REPORT"></action>
</intent-filter>
</receiver>
创建警报:
if (Exists(context)) return;
AlarmManager alarmMgr = (AlarmManager)context.GetSystemService(Context.AlarmService);
Intent intent = new Intent("com.companyname.APPNAME.SEND_REPORT");
PendingIntent alarmIntent = PendingIntent.GetBroadcast(context, 1337, intent, PendingIntentFlags.CancelCurrent);
Calendar calendar = Calendar.Instance;
calendar.TimeInMillis = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
alarmMgr.SetInexactRepeating(AlarmType.RtcWakeup, calendar.TimeInMillis, 1000 * 10, alarmIntent);
接收者:
public class AlarmReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Classes.Log.Write("Reports alarm received.");
PowerManager pm = (PowerManager)context.GetSystemService(Context.PowerService);
PowerManager.WakeLock wl = pm.NewWakeLock(WakeLockFlags.Partial, "SendReports");
wl.Acquire();
Classes.Network.SendReportsAsync();
wl.Release();
Classes.Log.Write("Reports alarm finished.");
}
}
ADB Logcat 日志:
07-23 11:08:46.723 1312 1863 V AlarmManager: Triggering alarm #0: 0 when =1595491710351 package =com.companyname.APPNAME operation =*walarm*:com.companyname.APPNAME.SEND_REPORT flags =0x0
接收器日志从未写入我的日志文件并且未发送任何报告,这意味着接收器从未被执行。
提前致谢。
更新:
这是在我将 Intent 更改为 typeof(AlarmReceiver) 之后,这就是输出
07-23 17:43:20.616 1312 1863 V AlarmManager: Triggering alarm #0: 0 when =1595515400614 package =com.companyname.ReportOne operation =*walarm*:com.companyname.ReportOne/md5057b6bb0fb853ea7a7a76aecdac4ee0b.AlarmReceiver flags =0x0
AlarmReceiver 仍然不会执行
更新 2:
这是启动警报的当前代码
public static void Init(Context context)
{
if (Exists(context)) return;
AlarmManager alarmMgr = (AlarmManager)context.GetSystemService(Context.AlarmService);
Intent intent = new Intent(context, typeof(AlarmReceiver));
PendingIntent alarmIntent = PendingIntent.GetBroadcast(context, 1337, intent, PendingIntentFlags.CancelCurrent);
// Set the alarm to start at 7:00 a.m.
Calendar calendar = Calendar.Instance;
calendar.TimeInMillis = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
Classes.Log.Write(string.Format("Initializing Alarm, next alarm will fire at {0}",
DateTimeOffset.FromUnixTimeMilliseconds(GetMilliseconds()).ToString("g")));
alarmMgr.SetInexactRepeating(AlarmType.RtcWakeup, calendar.TimeInMillis, 1000 * 10, alarmIntent);
}
所以经过一些研究我意识到在 Xamarin 中你不应该从清单文件中注册你的接收器,Xamarin.Android 为此提供了一个属性。
[BroadcastReceiver(Enabled = true, Exported = false)]
public class AlarmReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Classes.Log.Write("Reports alarm received.");
PowerManager pm = (PowerManager)context.GetSystemService(Context.PowerService);
PowerManager.WakeLock wl = pm.NewWakeLock(WakeLockFlags.Partial, "SendReports");
wl.Acquire();
//Classes.Network.SendReportsAsync();
wl.Release();
Classes.Log.Write("Reports alarm finished.");
}
}
所以我使用 AlarmManager 设置了一个警报,adb logcat 显示警报触发但它不会执行 Receiver 的 OnReceive 函数。
清单:
<receiver android:name=".AlarmReceiver" android:enabled="true">
<intent-filter>
<action android:name="com.companyname.APPNAME.SEND.REPORT"></action>
</intent-filter>
</receiver>
创建警报:
if (Exists(context)) return;
AlarmManager alarmMgr = (AlarmManager)context.GetSystemService(Context.AlarmService);
Intent intent = new Intent("com.companyname.APPNAME.SEND_REPORT");
PendingIntent alarmIntent = PendingIntent.GetBroadcast(context, 1337, intent, PendingIntentFlags.CancelCurrent);
Calendar calendar = Calendar.Instance;
calendar.TimeInMillis = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
alarmMgr.SetInexactRepeating(AlarmType.RtcWakeup, calendar.TimeInMillis, 1000 * 10, alarmIntent);
接收者:
public class AlarmReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Classes.Log.Write("Reports alarm received.");
PowerManager pm = (PowerManager)context.GetSystemService(Context.PowerService);
PowerManager.WakeLock wl = pm.NewWakeLock(WakeLockFlags.Partial, "SendReports");
wl.Acquire();
Classes.Network.SendReportsAsync();
wl.Release();
Classes.Log.Write("Reports alarm finished.");
}
}
ADB Logcat 日志:
07-23 11:08:46.723 1312 1863 V AlarmManager: Triggering alarm #0: 0 when =1595491710351 package =com.companyname.APPNAME operation =*walarm*:com.companyname.APPNAME.SEND_REPORT flags =0x0
接收器日志从未写入我的日志文件并且未发送任何报告,这意味着接收器从未被执行。
提前致谢。
更新: 这是在我将 Intent 更改为 typeof(AlarmReceiver) 之后,这就是输出
07-23 17:43:20.616 1312 1863 V AlarmManager: Triggering alarm #0: 0 when =1595515400614 package =com.companyname.ReportOne operation =*walarm*:com.companyname.ReportOne/md5057b6bb0fb853ea7a7a76aecdac4ee0b.AlarmReceiver flags =0x0
AlarmReceiver 仍然不会执行
更新 2:
这是启动警报的当前代码
public static void Init(Context context)
{
if (Exists(context)) return;
AlarmManager alarmMgr = (AlarmManager)context.GetSystemService(Context.AlarmService);
Intent intent = new Intent(context, typeof(AlarmReceiver));
PendingIntent alarmIntent = PendingIntent.GetBroadcast(context, 1337, intent, PendingIntentFlags.CancelCurrent);
// Set the alarm to start at 7:00 a.m.
Calendar calendar = Calendar.Instance;
calendar.TimeInMillis = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
Classes.Log.Write(string.Format("Initializing Alarm, next alarm will fire at {0}",
DateTimeOffset.FromUnixTimeMilliseconds(GetMilliseconds()).ToString("g")));
alarmMgr.SetInexactRepeating(AlarmType.RtcWakeup, calendar.TimeInMillis, 1000 * 10, alarmIntent);
}
所以经过一些研究我意识到在 Xamarin 中你不应该从清单文件中注册你的接收器,Xamarin.Android 为此提供了一个属性。
[BroadcastReceiver(Enabled = true, Exported = false)]
public class AlarmReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Classes.Log.Write("Reports alarm received.");
PowerManager pm = (PowerManager)context.GetSystemService(Context.PowerService);
PowerManager.WakeLock wl = pm.NewWakeLock(WakeLockFlags.Partial, "SendReports");
wl.Acquire();
//Classes.Network.SendReportsAsync();
wl.Release();
Classes.Log.Write("Reports alarm finished.");
}
}