【问题标题】:How to check battery saver is on in android API <21如何在 android API <21 中检查节电模式是否开启
【发布时间】:2016-01-21 12:36:25
【问题描述】:

今天我在地图中创建了一个应用程序,但是当电池保护程序打开时它崩溃了。

如何在电池保护程序开启时检查事件,对于每台设备,帮助我。谢谢

我试试这个,但在 API

                PowerManager powerManager = (PowerManager)
                        this.getSystemService(Context.POWER_SERVICE);
                if ( powerManager.isPowerSaveMode()) {
                    // Animations are disabled in power save mode, so just show a toast instead.
                    Toast.makeText(customer_textReport.this, "Vui lòng tắt chế độ tiết kiệm pin", Toast.LENGTH_SHORT).show();
                }
                else {

                    Intent intentvitri = new Intent(customer_textReport.this, CustomerGetLocation.class);
                    startActivityForResult(intentvitri, 111);
                }

【问题讨论】:

    标签: android


    【解决方案1】:

    查看谷歌开发者:PowerManager

    在左上角,您可以更改 API 级别。

    如您所见,isPowerSaveMode() 是在 API 21(Lollipop) 中添加的。

    所以它不适用于旧设备。

    【讨论】:

      【解决方案2】:

      在 Android 版本低于 5.0 (Lollipop) 的手机上,每个制造商的省电模式都不同。但是,如果您的目标版本是 5.0 或更高版本,您可以使用Android Developer中描述的 PowerManager

      【讨论】:

      【解决方案3】:

      嗯。这是在 GPS 中检查高 acurary 模式。这是支票:

         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                          try {
                              if (getLocationMode(getApplicationContext()) != 3) {
                                  tvmessage.setText("Please turn on GPS high Acurary");
                                  btcancel_Dialog.setText("OK");
                                  btcancel_Dialog.setOnClickListener(new OnClickListener() {
                                      @Override
                                      public void onClick(View v) {
                                          startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                                          dlg.dismiss();
                                      }
                                  });
                                  dlg.show();
                              } else {
      
                                  Intent intentvitri = new Intent(customer_textReport.this, CustomerGetLocation.class);
                                  startActivityForResult(intentvitri, 111);
                              }
                          } catch (Settings.SettingNotFoundException e) {
                              e.printStackTrace();
                          }
                      }
      

      GPS的getLocationMode方法及返回模式:

       private int getLocationMode(Context context) throws Settings.SettingNotFoundException {
          return Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
      
      }
      

      【讨论】:

        【解决方案4】:

        我最终得到了这个函数。

        注意:它不等于 isPowerSaveMode()。它更像是 isRunningOutOfPower() 或 canBePowerSaveMode()

        它检查 SDK >= 21 那么 isPowerSaveMode 功能是否可用。如果不是,则检查 GPS 模式是否不是 HIGH_ACCURACY 并且电池电量是否低于 15%,那么它“可能是”powerSaveMode。

        public static boolean couldBePowerSaveMode(Context context) {
        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
          if (powerManager != null) {
            return powerManager.isPowerSaveMode();
          }
        }
        
        if (getLocationMode(context) != Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) {
          return getBatteryPercentage(context) <= 15;
        }
        return getBatteryPercentage(context) <= 15;
        

        }

        【讨论】:

          【解决方案5】:

          试试这个(准备复制/粘贴):

          /**
           * Checks if device is in power save mode. For older versions that do not support this API, returns false.
           * @return true if it is, false otherwise.
           */
          @TargetApi(Build.VERSION_CODES.LOLLIPOP)
          private static boolean isPowerSaveMode(Context context)
          {
              if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
              {
                  PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
                  return powerManager.isPowerSaveMode();
              }
          
              // For older versions, we just say that device is not in power save mode
              return false;
          }
          

          【讨论】:

            猜你喜欢
            • 2017-02-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-01-09
            相关资源
            最近更新 更多