2

将来、特定の時点で起動する通知を実装しようとしていますが、正しく機能させるのに苦労しています。将来の指定された時間ではなく、すぐに起動します。以下は私のレシーバークラスです。別のクラスから 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);
    }
}
}
4

2 に答える 2

1

AndroidManifest.xml にレシーバーを登録しましたか?

<receiver android:name=".NotificationAlertReceiver "></receiver>
于 2013-08-29T17:11:12.917 に答える
0

アラームは AlarmType.RtcWakeup を使用しているため、「現在」変数にはスレッド時間ではなくリアルタイム (System.currentTimeMillis() など) を使用する必要があります。

また、ミリ秒を取得するために、秒に 10,000 を掛けています。正しい乗数は 1,000 です。

于 2013-08-29T17:31:34.007 に答える