Xamarin AlarmManager Android

Rya*_*her 4 c# android xamarin.android xamarin

我已经创建了一个快速的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);           
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

如果有人可以分享一些智慧,那将非常感激.

Che*_*ron 6

问题是你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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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);
        };
    }
}
Run Code Online (Sandbox Code Playgroud)