/// <summary>
    /// When battery level changes
    /// </summary>
    /// <param name="e"></param>
    protected virtual void OnBatteryChanged(BatteryChangedEventArgs e)
    {
      if (BatteryChanged == null)
        return;

      BatteryChanged(this, e);
    }
        /// <summary>
        /// When battery level changes
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnBatteryChanged(BatteryChangedEventArgs e)
        {
            if (BatteryChanged == null)
            {
                return;
            }

            BatteryChanged(this, e);
        }
    /// <summary>
    /// Receiver BR
    /// </summary>
    /// <param name="context"></param>
    /// <param name="intent"></param>
    public override void OnReceive(Context context, Intent intent)
    {
      if (BatteryLevelChanged == null)
        return;

      var args = new BatteryChangedEventArgs();




      var level = intent.GetIntExtra(BatteryManager.ExtraLevel, -1);
      var scale = intent.GetIntExtra(BatteryManager.ExtraScale, -1);


      args.RemainingChargePercent = (int)Math.Floor(level * 100D / scale);

      if (intent.Action == Intent.ActionBatteryLow)
        args.IsLow = true;
      else if (intent.Action == Intent.ActionBatteryOkay)
        args.IsLow = false;
      else
        args.IsLow = args.RemainingChargePercent <= 15;

      // Are we charging / charged? works on phones, not emulators must check how.
      int status = intent.GetIntExtra(BatteryManager.ExtraStatus, -1);
      var isCharging = status == (int)BatteryStatus.Charging || status == (int)BatteryStatus.Full;

      // How are we charging?
      var chargePlug = intent.GetIntExtra(BatteryManager.ExtraPlugged, -1);
      var usbCharge = chargePlug == (int)BatteryPlugged.Usb;
      var acCharge = chargePlug == (int)BatteryPlugged.Ac;
      bool wirelessCharge = false;
#if __ANDROID_17__
      if ((int)Build.VERSION.SdkInt >= 17)
      {
          wirelessCharge = chargePlug == (int)BatteryPlugged.Wireless;
      }
#endif
      isCharging = (usbCharge || acCharge || wirelessCharge);
      if (!isCharging)
      {
        args.PowerSource = PowerSource.Battery;
      }
      else if (usbCharge)
      {
        args.PowerSource = PowerSource.Usb;
      }
      else if (acCharge)
      {
        args.PowerSource = PowerSource.Ac;
      }
      else if (wirelessCharge)
      {
        args.PowerSource = PowerSource.Wireless;
      }
      else
      {
        args.PowerSource = PowerSource.Other;
      }


      if (isCharging)
        args.Status = Abstractions.BatteryStatus.Charging;
      else
      {
        switch (status)
        {
          case (int)BatteryStatus.Charging:
            args.Status = Abstractions.BatteryStatus.Charging;
            break;
          case (int)BatteryStatus.Discharging:
            args.Status = Abstractions.BatteryStatus.Discharging;
            break;
          case (int)BatteryStatus.Full:
            args.Status = Abstractions.BatteryStatus.Full;
            break;
          case (int)BatteryStatus.NotCharging:
            args.Status = Abstractions.BatteryStatus.NotCharging;
            break;
          default:
            args.Status = Abstractions.BatteryStatus.Unknown;
            break;
        }
      }
      BatteryLevelChanged(args);
    }
Esempio n. 4
0
        private void Mudanca_status_bateria(object sender, Plugin.Battery.Abstractions.BatteryChangedEventArgs e)
        {
            try
            {
                lbl_carga.Text = e.RemainingChargePercent.ToString() + "%";

                if (e.IsLow)
                {
                    lbl_bateria_fraca.Text      = "Bateria Fraca!";
                    lbl_bateria_fraca.TextColor = Color.Red;
                    lbl_bateria_fraca.HorizontalTextAlignment = TextAlignment.Center;
                    lbl_bateria_fraca.FontAttributes          = FontAttributes.Italic;
                }

                switch (e.Status)
                {
                case Plugin.Battery.Abstractions.BatteryStatus.Charging:
                    lbl_status.Text = "Carregando";
                    break;

                case Plugin.Battery.Abstractions.BatteryStatus.Discharging:
                    lbl_status.Text = "Descarregando";
                    break;

                case Plugin.Battery.Abstractions.BatteryStatus.Full:
                    lbl_status.Text = "Carregada";
                    break;

                case Plugin.Battery.Abstractions.BatteryStatus.NotCharging:
                    lbl_status.Text = "Sem carregar";
                    break;

                case Plugin.Battery.Abstractions.BatteryStatus.Unknown:
                    lbl_status.Text = "Desconhecido.";
                    break;
                }


                switch (e.PowerSource)
                {
                case Plugin.Battery.Abstractions.PowerSource.Ac:
                    lbl_source.Text = "Carregador";
                    break;

                case Plugin.Battery.Abstractions.PowerSource.Battery:
                    lbl_source.Text = "Bateria";
                    break;

                case Plugin.Battery.Abstractions.PowerSource.Usb:
                    lbl_source.Text = "USB";
                    break;

                case Plugin.Battery.Abstractions.PowerSource.Wireless:
                    lbl_source.Text = "Sem Fio";
                    break;

                case Plugin.Battery.Abstractions.PowerSource.Other:
                    lbl_source.Text = "Desconhecido";
                    break;
                }
            } catch (Exception ex)
            {
                DisplayAlert("Ops", ex.Message, "Ok");
            }
        }
 /// <summary>
 /// When battery level changes
 /// </summary>
 /// <param name="e"></param>
 protected virtual void OnBatteryChanged(BatteryChangedEventArgs e) =>
 BatteryChanged?.Invoke(this, e);