我正在尝试在未来的某个固定点实施通知,但我很难让它正常工作。它会立即触发,而不是在将来的指定时间触发。下面是我的接收器类。我从另一个类调用SetAlarm并传递一个时间间隔,即alertTime。有什么想法吗?感谢!!!
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Util;
namespace Test_Android
{
[BroadcastReceiver]
class NotificationAlertReceiver : BroadcastReceiver
{
public NotificationAlertReceiver()
{
}
public override void OnReceive(Context context, Intent intent)
{
PowerManager pm = (PowerManager)context.GetSystemService(Context.PowerService);
PowerManager.WakeLock w1 = pm.NewWakeLock (WakeLockFlags.Partial, "NotificationReceiver");
w1.Acquire ();
var nMgr = (NotificationManager)context.GetSystemService (Context.NotificationService);
var notification = new Notification (Resource.Drawable.loadIcon, "Arrival");
var pendingIntent = PendingIntent.GetActivity (context, 0, new Intent (context, typeof(MainActivity)), 0);
notification.SetLatestEventInfo (context, "Arrival", "Arrival", pendingIntent);
nMgr.Notify (0, notification);
w1.Release ();
}
public void CancelAlarm(Context context){
Intent intent = new Intent(context, this.Class);
PendingIntent sender = PendingIntent.GetBroadcast (context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.GetSystemService (Context.AlarmService);
alarmManager.Cancel (sender);
}
public void SetAlarm(Context context, int alertTime){
long now = SystemClock.CurrentThreadTimeMillis();
AlarmManager am = (AlarmManager) context.GetSystemService (Context.AlarmService);
Intent intent = new Intent(context, this.Class);
PendingIntent pi = PendingIntent.GetBroadcast (context, 0, intent, 0);
am.Set (AlarmType.RtcWakeup, now + ((long)(alertTime*10000)), pi);
}
}
}
答案 0 :(得分:1)
您是否在AndroidManifest.xml中注册了接收器?
<receiver android:name=".NotificationAlertReceiver "></receiver>
答案 1 :(得分:0)
由于您的闹钟正在使用AlarmType.RtcWakeup,因此您应该使用实时(例如System.currentTimeMillis())而不是线程时间来使用“now”变量。
此外,您将秒乘以10,000得到毫秒。正确的乘数是1,000。