Xamarin Android - 开关切换 On/Off 来自 MainActivity 的 AlarmReceiver 中通知的振动

Xamarin Android - Switch Toggle On/Off Vibration of a notification in AlarmReceiver from MainActivity

GrayBark says: "If your activity is killed off and the alarm is fired there will be no activity open to stop the ringtone." How to????

我做了一个简单的通知和切换按钮。

1. 起初,我没有包括开关按钮,但它工作正常。我的代码 像这样

2.

And then I finally added a switch toggle button. It worked pretty well when the app is active, minimized.. However, when it's killed, the notification stops and the app is stopped working. Code is this:

报警接收器

        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。如上所述:

If Toggle Vibration is Switched then MainActivity is killed BUT the Alarm Fires, therfore Alarm will not fire since BroadCastReceiver can't find whether the Toggle is Switched or Not

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。

Solution is.. make them communicate using Intent Strings.

  • MainActivity

        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());
    
    }