2016-07-06 63 views
1

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?

回答

0

最後,我能夠得到答案,謝謝上帝.. 首先,要了解問題......我的BroadCastReceiver似乎她依賴於MainActivity。正如上面提到的:

如果切換振動切換然後MainActivity被殺死,但火警,報警therfore不會火,因爲廣播接收器無法找到切換是否開啓或不

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。

解決方案是..讓他們使用意圖字符串進行通信。

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