前言
之前说了那么的电量优化理论,到现在自己都没有去做个小案例实践一下,这次博客的内容是根据实际的使用环境想出的一个小案例,方便自己去理解,电量优化该怎么用?
问题抛出:
为了省电,有些工作可以放当手机插上电源的时候去做。往往这样的情况非常多。像这些不需要及时地和用户交互的操作可以放到后面处理。比如:360手机助手,当充上电的时候,才会自动清理手机垃圾,自动备份上传图片、联系人等到云端。例如:拍照和图片处理是否可以做一些优化,假如现在手机的电量比较低,并且没有充电,拍照动作是要立马执行的,但是图片的处理是否可以放在用户插上手机充电器之后去处理?
首先我们得知道如何去获取到手机目前的充电状态:
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent batteryStatus = this.registerReceiver(null, filter); int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); boolean usbCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_USB); boolean acCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_AC); boolean wirelessCharge = false; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { wirelessCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS); } return (usbCharge || acCharge || wirelessCharge);
在上面的例子演示了如何立即获取到手机的充电状态,得到充电状态信息之后,我们可以有针对性的对部分代码做优化。比如我们可以判断只有当前手机为AC充电状态时才去执行一些非常耗电的操作.
下面给出一个针对上面案例的代码
小案例代码:
public class WaitForPowerActivity extends ActionBarActivity { public static final String LOG_TAG = "WaitForPowerActivity"; TextView mPowerMsg; ImageView mCheyennePic; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_power); mPowerMsg = (TextView) findViewById(R.id.cheyenne_txt); mCheyennePic = (ImageView) findViewById(R.id.cheyenne_img); Button theButtonThatTakesPhotos = (Button) findViewById(R.id.power_take_photo); theButtonThatTakesPhotos.setText(R.string.take_photo_button); final Button theButtonThatFiltersThePhoto = (Button) findViewById(R.id.power_apply_filter); theButtonThatFiltersThePhoto.setText(R.string.filter_photo_button); theButtonThatTakesPhotos.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { takePhoto(); // After we take the photo, we should display the filter option. theButtonThatFiltersThePhoto.setVisibility(View.VISIBLE); } }); theButtonThatFiltersThePhoto.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { applyFilter(); } }); } private void takePhoto() { // Make photo of Cheyenne appear. mPowerMsg.setText(R.string.photo_taken); mCheyennePic.setImageResource(R.drawable.cheyenne); } private void applyFilter() { //是否在充电 if(!checkForPower()){ mPowerMsg.setText("请充上电,再处理!"); return; } //实际开发中这边应该是对拍照保存下来的图片进行处理 mCheyennePic.setImageResource(R.drawable.pink_cheyenne); mPowerMsg.setText(R.string.photo_filter); } private boolean checkForPower(){ //获取电池的充电状态 IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent intent = this.registerReceiver(null,filter); //BatteryManager int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,-1); boolean usb = chargePlug ==BatteryManager.BATTERY_PLUGGED_USB;//usb充电 boolean ac = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;//交流电 //无线充电---API>=17 boolean wireless = false; if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN_MR1) { wireless = chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS; } return (usb||ac||wireless); } }
上面的案例就是当手机处于充电状态的时才会去执行处理图片的逻辑。
作者:hpc19950723 发表于2017/4/7 16:57:31 原文链接
阅读:78 评论:0 查看评论