Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

c# - Xamarin AlarmManager Android

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...