简体   繁体   中英

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????

I have made a simple notification and switch buttons.

1. At First, I didn't include switch buttons and it works fine. My code is like this one

  • 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.

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:

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 can I fix this? I can feel that my problem is right under my noses.. also, I've seen a similar problem to this but It's too blurry for me to understand How do I stop playing sound by switching between toggle button?

Finally, I was able to get the answer, Thanks be to God.. Firstly, to understand the problem... my BroadCastReceiver seemed that she depends on MainActivity. As it mentioned above:

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"

Therefore we should make the MainActivity depends on 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; } 
  • 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(); 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()); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM