2017-04-18 34 views
1

我看到和使用的這個鏈接:Creating And Scheduling Alarms Using AlarmManager In AndroidAndroid中(C#)開始AlarmManager

現在我有這樣的代碼:

namespace AlarmManage 
{ 
    public class MyBroadcastReceiver : BroadcastReceiver 
    { 
     public override void OnReceive(Context context, Intent intent) 
     { 
      Toast.MakeText(context, "Time Up... Now Vibrating !!!", 
        ToastLength.Long).Show(); 
      Vibrator vibrator = (Vibrator)context 
        .GetSystemService(Context.VibratorService); 
      vibrator.Vibrate(2000); 
     } 
    } 
} 

public void startAlertAtParticularTime() 
{ 
    // alarm first vibrate at 14 hrs and 40 min and repeat itself at ONE_HOUR interval 
    intent = new Intent(this, typeof(MyBroadcastReceiver)); 
    pendingIntent = PendingIntent.GetBroadcast(
      this, 280192, intent, PendingIntentFlags.CancelCurrent); 
    Java.Util.Calendar calendar = Java.Util.Calendar.Instance; 
    calendar.TimeInMillis = Java.Lang.JavaSystem.CurrentTimeMillis(); 
    calendar.Set(Java.Util.CalendarField.HourOfDay, 14); 
    calendar.Set(Java.Util.CalendarField.Minute, 49); 
    alarmManager = (AlarmManager)GetSystemService(AlarmService); 
    alarmManager.SetRepeating(AlarmType.RtcWakeup, calendar.TimeInMillis, 
      AlarmManager.IntervalHour, pendingIntent); 
    Toast.MakeText(this, "Alarm will vibrate at time specified", ToastLength.Long).Show(); 
} 

我還設置 「SET-ALARM」 的清單和 「VIBRATE」。

我的問題:我沒有看到輸出「時間最多......現在振動!

回答

1

我認爲問題是,你沒有成功申報你的MyBroadcastReceiver,在xamarin,我們可以使用:

[BroadcastReceiver(Enabled = true)] 

宣佈該廣播這就像宣稱它在傳統Android清單是這樣的:

<receiver android:name="MyBroadcastReceiver" /> 

所以,MyBroadcastReceiver應該是李柯本:

[BroadcastReceiver(Enabled = true)] 
public class MyBroadcastReceiver : BroadcastReceiver 
{ 
    public override void OnReceive(Context context, Intent intent) 
    { 
     Toast.MakeText(context, "Time Up... Now Vibrating !!!", 
       ToastLength.Long).Show(); 
     Vibrator vibrator = (Vibrator)context 
       .GetSystemService(Context.VibratorService); 
     vibrator.Vibrate(2000); 
    } 
} 

現在你可以敬酒:「時間最多......現在振動!!!」