Xamarin Android-如何使用BroadcastReceiver计划和报警 [英] Xamarin Android - How to schedule and alarm with a BroadcastReceiver

查看:70
本文介绍了Xamarin Android-如何使用BroadcastReceiver计划和报警的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我在这里问是因为我没有在Xamarin论坛上获得帮助),我正在使用以下代码创建警报:

(I'm asking here because I didn't get help at Xamarin forums) I'm creating an alarm with this code:

Intent alarmIntent = new Intent(context, typeof(AlarmReceiver));
    notificationClickIntent = PendingIntent.GetActivity(context, 0, new Intent(), 0);
    pendingIntent = PendingIntent.GetBroadcast(context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
    am = (AlarmManager)Android.App.Application.Context.GetSystemService(Context.AlarmService);

    DateTime setTime = new DateTime(temp.Ticks + offset); //temp is the current time where seconds field = 0
    if ((int)Build.VERSION.SdkInt >= 21) //my device enters this case
    {
        AlarmManager.AlarmClockInfo info = new AlarmManager.AlarmClockInfo(setTime.Ticks, notificationClickIntent);
        am.SetAlarmClock(info, pendingIntent);
    }
    else {
        am.SetExact(AlarmType.RtcWakeup, setTime.Ticks, notificationClickIntent);
    }

在调用该代码之前,我的类确保这些代码也已执行:

Before that code is called, my class makes sure that these have also been executed:

ComponentName receiver = new ComponentName(context, Java.Lang.Class.FromType(typeof(AlarmReceiver)));
        PackageManager pm = context.PackageManager;
        pm.SetComponentEnabledSetting(receiver, ComponentEnabledState.Enabled, ComponentEnableOption.DontKillApp);

        Intent alarmIntent = new Intent(context, typeof(AlarmReceiver));
        notificationClickIntent = PendingIntent.GetActivity(context, 0, new Intent(), 0);
        pendingIntent = PendingIntent.GetBroadcast(context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
        am = (AlarmManager)Android.App.Application.Context.GetSystemService(Context.AlarmService);

这是我的接收者:

[BroadcastReceiver (Process = ":remote")]
    public class AlarmReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            Console.WriteLine("alarm fired");
            Toast.MakeText(context, "Received intent!", ToastLength.Short).Show();
        }
    }

好的,因此Xamarin已正确注册了接收者。我知道这是因为,如果我给AlarmClockInfo提供了不正确的滴答值(一个超出DateTime滴答范围的值),警报会立即响起并调用我的OnReceive方法。但是,当我给它一个刻度值时,比如说比当前时间提前一分钟,闹钟不会响起。也许时间错了?...似乎不是,因为我已经记录了系统的下一个预定警报的时间,并且它会在设置警报的同时返回报告。有什么想法吗?

Okay, so the receiver is being registered by Xamarin correctly. I know this because if I give an incorrect tick value to AlarmClockInfo (a value outside of DateTime's tick range) the alarm goes off immediately and my OnReceive method is called. However when I give it a tick value, say a minute ahead of the current time, the alarm doesn't go off. Maybe the time is wrong?... Doesn't seem so because I have logged the time of the the system's next scheduled alarm and it reports back with the same time I set it for. Any thoughts?

编辑:所以我已经有一个可以正确执行所有操作的android应用。当我将其转换为Xamarin和C#时,它将不再起作用。

So I already have an android app that performs all this correctly. When I convert it to Xamarin and C#, it no longer works.

推荐答案

这就是我在其中创建本地通知的方式我的Xamarin应用。

This is how I'm creating a local notification in my Xamarin app.

DateTime time = ... // whatever time
AlarmManager manager = (AlarmManager)context.GetSystemService(Context.AlarmService);

Java.Util.Calendar calendar = Java.Util.Calendar.Instance;
calendar.TimeInMillis = Java.Lang.JavaSystem.CurrentTimeMillis();

calendar.Set(time.Year, time.Month - 1, time.Day, time.Hour, time.Minute, 0);
manager.SetRepeating(AlarmType.RtcWakeup, calendar.TimeInMillis,
AlarmManager.IntervalDay, pendingIntent);

这是BroadcastReceiver类:

And here is the BroadcastReceiver class:

[BroadcastReceiver]
public class AlarmReceiver : BroadcastReceiver
{

    public override void OnReceive(Context context, Intent intent)
    {
        NotificationManager nManager = (NotificationManager)context.GetSystemService(Context.NotificationService);

        Intent repeatingIntent;

        // Here I'm opening two different Activities based on condition
        if (CommonUtils.isLoggedIn()))
        {
            repeatingIntent = new Intent(context, typeof(MainActivity));
            repeatingIntent.PutExtra(MainActivity.SELECT_TAB, 1);
        }
        else
        {
            repeatingIntent = new Intent(context, typeof(SplashActivity));
        }

        repeatingIntent.SetFlags(ActivityFlags.ClearTop);

        PendingIntent pIntent = PendingIntent.GetActivity(context, 100, repeatingIntent, PendingIntentFlags.UpdateCurrent);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .SetContentIntent(pIntent)
            .SetSmallIcon(Resource.Drawable.az_logo_small)
            .SetColor(ContextCompat.GetColor(context, Resource.Color.PrimaryColor))
            .SetContentTitle(CommonUtils.MAIN_TITLE)
            .SetContentText(UIMessages.VITAL_REMINDER)
            .SetAutoCancel(true);

        nManager.Notify(100, builder.Build());
    }
}

,在AndroidManifest.xml中,您需要此权限

and in AndroidManifest.xml, you need this permission

<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />

还需要在AndroidManifest.xml中注册BroadcastReceiver

also need the register the BroadcastReceiver in AndroidManifest.xml

<application android:label="YourAppName" android:largeHeap="true" android:icon="@drawable/ic_launcher">
    <receiver android:name=".AlarmReceiver"></receiver>
</application>

希望有帮助。

这篇关于Xamarin Android-如何使用BroadcastReceiver计划和报警的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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