GrayBark說:「如果您的活動中斷並且警報被觸發,則不會打開任何活動來停止鈴聲。如何?Xamarin安卓系統 - 開關切換開/關MainActivity中AlarmReceiver中通知的振動
我做了一個簡單的通知和切換按鈕。
1.首先,我沒有包含開關按鈕,它工作正常。我的代碼 是這樣一個
MainActivity
private void MRemindMe_Click(object sender, EventArgs e) { //StartReminder 1-6hrs intRemind = new Intent(this, typeof(AlarmReceiver)); pendInt = PendingIntent.GetBroadcast(this, 0, intRemind, PendingIntentFlags.UpdateCurrent); alarmManager = (AlarmManager)GetSystemService(AlarmService); alarmManager.SetInexactRepeating(AlarmType.ElapsedRealtimeWakeup, SystemClock.ElapsedRealtime() + (1000 * mSeekBar.Progress), (1000 * mSeekBar.Progress), pendInt); mCancelNotif.Visibility = ViewStates.Visible; }
AlarmReceiver
public override void OnReceive(Context context, Intent intent) { Intent Intent = new Intent(context, typeof(MainActivity)); PendingIntent BuildPendingIntent = PendingIntent.GetActivity(context, 0, Intent, PendingIntentFlags.CancelCurrent); manager = NotificationManager.FromContext(context); ISharedPreferences pref = Application.Context.GetSharedPreferences("MyApp", FileCreationMode.Private); ISharedPreferencesEditor editor = pref.Edit(); builder = new NotificationCompat.Builder(context) .SetAutoCancel(true) .SetContentIntent(BuildPendingIntent) .SetContentTitle("Remind Me!") .SetTicker("Checklist: Remind Me!") .SetContentText("It's time to check your CheckList!") .SetSmallIcon(Resource.Drawable.Icon) .SetVisibility((int)NotificationVisibility.Public) .SetFullScreenIntent(BuildPendingIntent, true) .SetPriority((int)NotificationPriority.High) .SetDefaults((int)NotificationDefaults.All); manager.Notify(0, builder.Build()); }
2.
然後我終於添加了一個開關切換按鈕。當應用程序處於活動狀態時,它工作得很好,最小化。但是,當它被殺死時,通知停止並且應用程序停止工作。 代碼是這樣的:
AlarmReceiver
public override void OnReceive(Context context, Intent intent)
{
Intent Intent = new Intent(context, typeof(MainActivity));
PendingIntent BuildPendingIntent = PendingIntent.GetActivity(context, 0, Intent, PendingIntentFlags.CancelCurrent);
manager = NotificationManager.FromContext(context);
ISharedPreferences pref = Application.Context.GetSharedPreferences("MyApp", FileCreationMode.Private);
ISharedPreferencesEditor editor = pref.Edit();
builder = new NotificationCompat.Builder(context)
.SetAutoCancel(true)
.SetContentIntent(BuildPendingIntent)
.SetContentTitle("Remind Me!")
.SetTicker("Checklist: Remind Me!")
.SetContentText("It's time to check your CheckList!")
.SetSmallIcon(Resource.Drawable.Icon)
.SetVisibility((int)NotificationVisibility.Public)
.SetFullScreenIntent(BuildPendingIntent, true)
.SetPriority((int)NotificationPriority.High);
//added Switch Toggle
if (MainActivity.mvibrateSW.Checked)
{
builder.SetDefaults((int)NotificationDefaults.Vibrate);
}
manager.Notify(0, builder.Build());
}
我該如何解決這個問題?我可以感覺到我的問題是在我的鼻子下。另外,我也看到類似的問題,但它對我來說太模糊了 How do I stop playing sound by switching between toggle button?