gpt4 book ai didi

c# - Xamarin AlarmManager Android

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:35:29 26 4
gpt4 key购买 nike

我创建了一个快速的 xamarin android 项目。最终我会想把下面的学习应用到一个在 android 和 ios 之间共享的 xamarin 表单项目中。现在我只关注 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

如果有人能分享一些智慧,将不胜感激。

最佳答案

问题是您的 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);
};
}
}

关于c# - Xamarin AlarmManager Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36460571/

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