Xamarin 警报管理器 Android [英] Xamarin AlarmManager Android

查看:24
本文介绍了Xamarin 警报管理器 Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个快速的 xamarin android 项目.最终,我想学习下面的知识并将其应用于在 android 和 ios 之间共享的 xamarin 表单项目.目前我只关注 Android 方面的事情.

我一直在努力学习如何安排本地通知在未来某个时间出现.做了一个快速扔掉的应用程序,下面是我写的 AlarmReceiver 类,以及下面的 MainActivity.cs.

class AlarmReceiver : BroadcastReceiver{公共覆盖无效 OnReceive(上下文上下文,意图意图){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);//生成一个只有短文本和小图标的通知var builder = new Notification.Builder(context).SetContentIntent(contentIntent).SetContentTitle(标题).SetContentText(消息).SetWhen(Java.Lang.JavaSystem.CurrentTimeMillis()).SetAutoCancel(true);var 通知 = builder.Build();manager.Notify(0, 通知);}}公共类 MainActivity : 活动{//我们通知的唯一 ID:私有静态只读 int ButtonClickNotificationId = 1000;//按钮被点击的次数(从第一次点击开始):私有整数计数 = 1;protected override void OnCreate(Bundle bundle){base.OnCreate(捆绑);SetContentView(Resource.Layout.Main);//显示Hello World,点击我!"按钮并注册其事件处理程序:按钮按钮 = FindViewById

当我逐步调试并调试时,似乎没有调用我的 OnReceive 方法.

我一直在关注这篇文章,因为我很难通过谷歌搜索来研究如何正确地做到这一点.此处:预定通知

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

解决方案

问题是你的 BroadcastReceiver 没有 [BroadcastReceiver] 属性.

此代码有效:

AlarmReceiver.cs

[广播接收器]公共类警报接收器:广播接收器{公共覆盖无效 OnReceive(上下文上下文,意图意图){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,结果意图,PendingIntentFlags.CancelCurrent);无功建造者 =新的 Notification.Builder(上下文).SetContentTitle(标题).SetContentText(消息).SetSmallIcon(Resource.Drawable.Icon).SetDefaults(NotificationDefaults.All);builder.SetContentIntent(待定);var 通知 = builder.Build();var manager = NotificationManager.FromContext(context);manager.Notify(1337, 通知);}}

MainActivity.cs

[Activity(Label = "App3", MainLauncher = true, Icon = "@drawable/icon")]公共类 MainActivity : 活动{protected override void OnCreate(Bundle bundle){base.OnCreate(捆绑);SetContentView(Resource.Layout.Main);var button = FindViewById

I've created a quick xamarin android project. Eventually I will want to take the learning below and apply it to a xamarin forms project that is shared between android and ios. For now I'm just focusing on the Android side of things.

I've been trying to learn how to schedule a local notification to appear at some time in the future. Made a quick throw away application, below is the AlarmReceiver Class I've written, as well as the MainActivity.cs below.

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);           
    }
}

When I step through and debug this, it appears that my OnReceive method is not being called.

I was following this article as I'm having a really tough time researching how to do this correctly via google searches. Here: Scheduled Notifications

If anyone could share some wisdom, would greatly appreciate it.

解决方案

The problem is that your BroadcastReceiver does not have the [BroadcastReceiver] attribute.

This code works:

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);
        };
    }
}

这篇关于Xamarin 警报管理器 Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆