Android 电池 温度 屏幕亮度设计

jopen 10年前

1363250996_9297.jpg

import android.app.Activity;  import android.content.BroadcastReceiver;  import android.content.ContentResolver;  import android.content.Context;  import android.content.Intent;  import android.content.IntentFilter;  import android.content.SharedPreferences;  import android.content.SharedPreferences.Editor;  import android.net.Uri;  import android.net.wifi.WifiManager;  import android.os.BatteryManager;  import android.os.Bundle;  import android.os.PowerManager;  import android.provider.Settings;  import android.provider.Settings.SettingNotFoundException;  import android.view.View;  import android.view.View.OnClickListener;  import android.view.WindowManager;  import android.widget.ImageView;  import android.widget.TextView;  import android.widget.Toast;  import android.widget.ToggleButton;  public class BatteryActivity extends Activity {      /** Called when the activity is first created. */            private IntentFilter   mIntentFilter;      private TextView mHealth;      private TextView mLevel;      private TextView mVoltage;      private TextView mTemperature;      private ImageView image;      private ToggleButton btn_save,btn_sleep;      private final static String ShareState="ModeState";      private SharedPreferences sp ;      private boolean mode_flag=false;      private WifiManager mWifiManager;      private BroadcastReceiver mIntentReceiver = new BroadcastReceiver()       {              @Override          public void onReceive(Context context, Intent intent) {              // TODO Auto-generated method stub              String action = intent.getAction();              if (action.equals(Intent.ACTION_BATTERY_CHANGED))               {                  int scale = intent.getIntExtra("scale", 100);                  int level=intent.getIntExtra("level", 0);                      if(level<10)                  {                      image.setImageResource(R.drawable.battery_00);                  }else if(level>=10&&level<20)                  {                      image.setImageResource(R.drawable.battery_10);                  }                  else if(level>=20&&level<30)                  {                      image.setImageResource(R.drawable.battery_20);                  }                  else if(level>=30&&level<40)                  {                      image.setImageResource(R.drawable.battery_30);                  }                  else if(level>=40&&level<50)                  {                      image.setImageResource(R.drawable.battery_40);                  }                  else if(level>=50&&level<60)                  {                      image.setImageResource(R.drawable.battery_50);                  }                  else if(level>=60&&level<70)                  {                      image.setImageResource(R.drawable.battery_60);                  }                  else if(level>=70&&level<80)                  {                      image.setImageResource(R.drawable.battery_70);                  }                  else if(level>=80&&level<90)                  {                      image.setImageResource(R.drawable.battery_80);                  }                  else if(level>=90&&level<100)                  {                      image.setImageResource(R.drawable.battery_90);                  }                  else                      image.setImageResource(R.drawable.battery_100);                       mLevel.setText("剩余电量:" + String.valueOf(level * 100 / scale) + "%");                  mVoltage.setText("电池电压:" + intent.getIntExtra("voltage", 0) +"mV");                  mTemperature.setText("电池温度 :" +tenthsToFixedString((intent.getIntExtra("temperature", 0))));                  int health = intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN);                  String healthString;                  if (health == BatteryManager.BATTERY_HEALTH_GOOD) {                      healthString = "非常健康";                  } else if (health == BatteryManager.BATTERY_HEALTH_OVERHEAT) {                      healthString = "BATTERY_HEALTH_OVERHEAT";                  } else if (health == BatteryManager.BATTERY_HEALTH_DEAD) {                      healthString = "BATTERY_HEALTH_DEAD";                  } else if (health == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE) {                      healthString = "BATTERY_HEALTH_OVER_VOLTAGE";                  } else if (health == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE) {                      healthString = "BATTERY_HEALTH_UNSPECIFIED_FAILURE";                  }  else {                      healthString = "不清楚";                  }                                    mHealth.setText("健康状态 :"+healthString);              }          }                };      @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.battery);                    mIntentFilter = new IntentFilter();          mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);          sp= getSharedPreferences(ShareState, MODE_PRIVATE);          mWifiManager=(WifiManager) this.getSystemService(Context.WIFI_SERVICE);                         mLevel=(TextView) findViewById(R.id.level);          mHealth=(TextView) findViewById(R.id.health);          mTemperature=(TextView) findViewById(R.id.temperature);          mVoltage=(TextView) findViewById(R.id.voltage);          image=(ImageView) findViewById(R.id.pleve);          btn_save=(ToggleButton) findViewById(R.id.save);          btn_sleep=(ToggleButton) findViewById(R.id.sleep);                    mode_flag=sp.getBoolean("Save_KEY", true);          if(mode_flag)              btn_save.setChecked(true);          else              btn_save.setChecked(false);          mode_flag=sp.getBoolean("Sleep_KEY", true);          if(mode_flag)              btn_sleep.setChecked(true);          else              btn_sleep.setChecked(false);          registerReceiver(mIntentReceiver, mIntentFilter);                       btn_save.setOnClickListener(new OnClickListener() {                            @Override              public void onClick(View v) {                  // TODO Auto-generated method stub                  //存入数据                  Editor editor = sp.edit();                                               if(btn_save.isChecked())                  {                      Toast.makeText(BatteryActivity.this, "touch in", 1000).show();                      stopAutoBrightness(BatteryActivity.this);                      setBrightness(BatteryActivity.this, 65);                      if(mWifiManager.isWifiEnabled())                          mWifiManager.setWifiEnabled(false);                            editor.putBoolean("Save_KEY", true);                        editor.commit();                                        }                  else                  {                      Toast.makeText(BatteryActivity.this, "touch out", 1000).show();                       setBrightness(BatteryActivity.this, 125);                      mWifiManager.setWifiEnabled(true);                      startAutoBrightness(BatteryActivity.this) ;                      editor.putBoolean("Save_KEY", false);                      editor.commit();                  }              }          });          btn_sleep.setOnClickListener(new OnClickListener() {                            @Override              public void onClick(View v) {                  // TODO Auto-generated method stub                  //存入数据                  Editor editor = sp.edit();                                         if(btn_sleep.isChecked())                  {                      setAirplaneModeOn(BatteryActivity.this,true);                      editor.putBoolean("Sleep_KEY", true);                      editor.commit();                  }                  else                  {                      setAirplaneModeOn(BatteryActivity.this,false);                      editor.putBoolean("Sleep_KEY", false);                      editor.commit();                  }                                    if(getAirplaneMode(BatteryActivity.this))                  {                      Toast.makeText(BatteryActivity.this, "AirMode", 1000).show();                  }                  else                      Toast.makeText(BatteryActivity.this, "NotAirMode", 1000).show();                }          });      }            private  String tenthsToFixedString(int x) {          int tens = x / 10;          return Integer.toString(tens) + "." + (x - 10 * tens);      }    /*        * 判断手机是否是飞行模式      * @param context      * @return      */      public static boolean getAirplaneMode(Context context){      int isAirplaneMode = Settings.System.getInt(context.getContentResolver(),      Settings.System.AIRPLANE_MODE_ON, 0) ;      return (isAirplaneMode == 1)?true:false;      }            /**      * 设置手机飞行模式      * @param context      * @param enabling true:设置为飞行模式 false:取消飞行模式      */      public static void setAirplaneModeOn(Context context,boolean enabling) {      Settings.System.putInt(context.getContentResolver(),      Settings.System.AIRPLANE_MODE_ON,enabling ? 1 : 0);      Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);      intent.putExtra("state", enabling);      context.sendBroadcast(intent);      }            //------------省电亮度调节-------------------      public static void setBrightness(Activity activity, int brightness) {                               WindowManager.LayoutParams lp = activity.getWindow().getAttributes();                        lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);           //    Log.d("lxy", "set  lp.screenBrightness == " + lp.screenBrightness);                    activity.getWindow().setAttributes(lp);            //save          ContentResolver resolver = activity.getContentResolver();            saveBrightness( resolver, brightness);                    }            public static void saveBrightness(ContentResolver resolver, int brightness) {                          Uri uri = android.provider.Settings.System.getUriFor("screen_brightness");                        android.provider.Settings.System.putInt(resolver, "screen_brightness", brightness);                                             resolver.notifyChange(uri, null);           }       public static int getScreenBrightness(Activity activity) {                         int nowBrightnessValue = 0;                        ContentResolver resolver = activity.getContentResolver();                        try{                            nowBrightnessValue = android.provider.Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS);                        }                    catch(Exception e) {                            e.printStackTrace();                        }                        return nowBrightnessValue;             }             /** * 停止自动亮度调节 */              public static void stopAutoBrightness(Activity activity) {                Settings.System.putInt(activity.getContentResolver(),                        Settings.System.SCREEN_BRIGHTNESS_MODE,                        Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);         }             /** * 开启亮度自动调节 *                     * @param activity */                               public static void startAutoBrightness(Activity activity) {                          Settings.System.putInt(activity.getContentResolver(),                                      Settings.System.SCREEN_BRIGHTNESS_MODE,                                      Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);                    }             /** * 判断是否开启了自动亮度调节 */              public static boolean isAutoBrightness(ContentResolver aContentResolver) {              boolean automicBrightness = false;              try{                  automicBrightness = Settings.System.getInt(aContentResolver,                            Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;              }          catch(SettingNotFoundException e)          {                 e.printStackTrace();              }              return automicBrightness;        }   }