Xamarin 警报管理器 Android
Posted
技术标签:
【中文标题】Xamarin 警报管理器 Android【英文标题】:Xamarin AlarmManager Android 【发布时间】:2016-07-27 09:55:20 【问题描述】:我创建了一个快速的 xamarin android 项目。最终我会想把下面的学习应用到一个在 android 和 ios 之间共享的 xamarin forms 项目中。现在我只关注 Android 方面的事情。
我一直在尝试了解如何安排本地通知在未来某个时间出现。做了一个快速扔掉的应用程序,下面是我写的AlarmReceiver类,以及下面的MainActivity.cs。
class AlarmReceiver : BroadcastReceiver
public override void OnReceive(Context context, Intent intent)
var message = intent.GetStringExtra("message");
var title = intent.GetStringExtra("title");
var notIntent = new Intent(context, typeof(MainActivity));
var contentIntent = PendingIntent.GetActivity(context, 0, notIntent, PendingIntentFlags.CancelCurrent);
var manager = NotificationManager.FromContext(context);
//Generate a notification with just short text and small icon
var builder = new Notification.Builder(context)
.SetContentIntent(contentIntent)
.SetContentTitle(title)
.SetContentText(message)
.SetWhen(Java.Lang.JavaSystem.CurrentTimeMillis())
.SetAutoCancel(true);
var notification = builder.Build();
manager.Notify(0, notification);
public class MainActivity : Activity
// Unique ID for our notification:
private static readonly int ButtonClickNotificationId = 1000;
// Number of times the button is tapped (starts with first tap):
private int count = 1;
protected override void OnCreate(Bundle bundle)
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
// Display the "Hello World, Click Me!" button and register its event handler:
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += ButtonOnClick;
// Handler for button click events.
private void ButtonOnClick(object sender, EventArgs eventArgs)
Intent alarmIntent = new Intent(Application.Context, typeof(AlarmReceiver));
alarmIntent.PutExtra("message", "This is my test message!");
alarmIntent.PutExtra("title", "This is my test title!");
PendingIntent pendingIntent = PendingIntent.GetBroadcast(Application.Context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
AlarmManager alarmManager = (AlarmManager)Application.Context.GetSystemService(Context.AlarmService);
alarmManager.Set(AlarmType.ElapsedRealtime, SystemClock.ElapsedRealtime() + 1 * 1000, pendingIntent);
当我逐步调试它时,我的 OnReceive 方法似乎没有被调用。
我一直在关注这篇文章,因为我很难通过谷歌搜索来研究如何正确地做到这一点。这里:Scheduled Notifications
如果有人能分享一些智慧,将不胜感激。
【问题讨论】:
【参考方案1】:问题是您的BroadcastReceiver
没有[BroadcastReceiver
] 属性。
此代码有效:
AlarmReceiver.cs
[BroadcastReceiver]
public class AlarmReceiver : BroadcastReceiver
public override void OnReceive(Context context, Intent intent)
var message = intent.GetStringExtra("message");
var title = intent.GetStringExtra("title");
var resultIntent = new Intent(context, typeof(MainActivity));
resultIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);
var pending = PendingIntent.GetActivity(context, 0,
resultIntent,
PendingIntentFlags.CancelCurrent);
var builder =
new Notification.Builder(context)
.SetContentTitle(title)
.SetContentText(message)
.SetSmallIcon(Resource.Drawable.Icon)
.SetDefaults(NotificationDefaults.All);
builder.SetContentIntent(pending);
var notification = builder.Build();
var manager = NotificationManager.FromContext(context);
manager.Notify(1337, notification);
MainActivity.cs
[Activity(Label = "App3", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
protected override void OnCreate(Bundle bundle)
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
var button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += delegate
var alarmIntent = new Intent(this, typeof(AlarmReceiver));
alarmIntent.PutExtra("title", "Hello");
alarmIntent.PutExtra("message", "World!");
var pending = PendingIntent.GetBroadcast(this, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
var alarmManager = GetSystemService(AlarmService).JavaCast<AlarmManager>();
alarmManager.Set(AlarmType.ElapsedRealtime, SystemClock.ElapsedRealtime() + 5*1000, pending);
;
【讨论】:
好的,我们正在取得进展。是的,在添加 [BroadcastReceiver] 之后,我看到调试器在 OnReceive 方法中达到了我的断点。但是通知本身仍未出现在我的测试中。我们现在大约完成了一半。 似乎没有为它设置任何图标时它不会显示出来。让我更新我的答案。 如何以特定分钟的时间间隔重复警报 @sudharsanchandrasekaran 创建一个新问题来描述您的问题。以上是关于Xamarin 警报管理器 Android的主要内容,如果未能解决你的问题,请参考以下文章
Android 警报管理器 setExactAndAllowWhileIdle() 在打盹模式下的 Android 7.0 Nougat 中不起作用