我想通過AlarmManager定期調用線程。問題是,在給定的時間段之後,OnReceive()會被調用兩次(爲什麼?),然後再也不會再次調用。我在這裏做錯了什麼?
我與Xamarin(C#)Android版API級別從MainActivity工作23AlarmManager.SetRepeating not working properly
代碼:
Alarm alarm = new Alarm();
RegisterReceiver (alarm, new IntentFilter ("com.company.BROADCAST"));
alarm.SetAlarm(this);
報警類別:
[BroadcastReceiver]
[IntentFilter(new string[] {"com.company.BROADCAST"})]
public class Alarm : BroadcastReceiver
{
public override void OnReceive(Context c, Intent i)
{
Toast.MakeText (c, "hi", ToastLength.Short).Show();
}
public void SetAlarm(Context context)
{
AlarmManager am = (AlarmManager)context.GetSystemService(Context.AlarmService);
Intent i = new Intent("com.company.BROADCAST");
PendingIntent pi = PendingIntent.GetBroadcast(context, 0, i, 0);
am.SetRepeating(AlarmType.ElapsedRealtimeWakeup, SystemClock.ElapsedRealtime() + 1000, 1000 * 2, pi);
}
}
感謝您的幫助,我不知道這一點。你有什麼想法爲什麼警報不重複? – AllRight
我想說2000ms的重複間隔太小。 Android通常會將小於1分鐘的間隔擴展到一分鐘。讓我確認,雖然 – matthewrdev
@AllRight查看更新的答案。 – matthewrdev