Xamarin Android - 从 MainActivity 切换 AlarmReceiver 中通知的开/关振动
本文关键字:通知 Android MainActivity AlarmReceiver 切换 Xamarin | 更新日期: 2023-09-27 17:55:20
GrayBark说:"如果你的活动被杀死并且警报被触发,那么就不会有活动打开来停止铃声。如何????
我已经做了一个简单的通知和切换按钮。
1.起初,我没有包括开关按钮,它工作正常。我的代码 就像这个
-
主活动
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; }
-
报警接收器
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()); }
阿拉伯数字。
然后我终于添加了一个开关切换按钮。当应用程序处于活动状态时,它运行良好,最小化。但是,当它被杀死时,通知会停止,应用程序会停止工作。代码是这样的:
报警接收器
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());
}
我该如何解决这个问题?我能感觉到我的问题就在我眼皮底下。另外,我看到了与此类似的问题,但它太模糊了,我无法理解如何通过在切换按钮之间切换来停止播放声音?
最后,我能够得到答案,感谢上帝。首先,要了解问题...我的BroadCastReceiver似乎依赖于MainActivity。如上所述:
如果切换振动被切换,则主活动被终止,但警报被触发,因此警报不会触发,因为 BroadCastReceiver 找不到
切换是否切换
To put it Simply
Switch is On (SetVibration = true)
↳ Click to Turn On alarm
↳ Alarm Service is fired
↳ You want to exit App? = Yes
↳ Alarm Still Firing (but Where is Toggle? is it On/Off?)
↳ Alarm Dies and "Unfortunately, App is not Working"
因此,我们应该使 MainActivity 依赖于 BroadCastReceiver。
解决方案是..让他们使用意图字符串进行通信。
主活动
private void MRemindMe_Click(object sender, EventArgs e) { intRemind = new Intent(this, typeof(AlarmReceiver)); //ToggleSwitch if (mvibrateSW.Checked) { intRemind.PutExtra("vChecked", "On"); } //StartReminder 1-6hrs 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; }
报警接收器
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(); string VOn = intent.GetStringExtra("vChecked"); 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); //Receive Command by String string OnVib = intent.GetStringExtra("vChecked"); if (OnVib == "On") { builder.SetDefaults((int)NotificationDefaults.Vibrate); } else { builder.SetDefaults((int)NotificationDefaults.Lights); } manager.Notify(0, builder.Build()); }