Hands-on small projects (5) security guard _ Power Management, hands-on security guard
I have always wanted to make an animation of the battery in a foreign toolbox. Today I want to make a look at the interface like this. I have made the basic part, and it is still mobile guard.
① Changes in the volume of the mobile phone that uses Intent
Battery power has been a concern for mobile phone users, and in the android system, specifically provides a message Action-ACTION_BATTERY_CHANGED to get battery power.
Back, in the demo, since I want to get the system information, register a broadcast first to get the BatteryReceiver information. four major components of java broadcasting first register the information I want to obtain in the configuration file, all on the UI, and use a map set to install our information through handler. dispatchMessage passed the information ..................
Package com. example. bettary. recevier; import java. util. hashMap; import java. util. map; import com. example. bettary. batteryManagerActivity; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. OS. batteryManager; import android. OS. message;/*** @ author xiaoxin007 * 5:53:01 * broadcast receiver of TODO battery */public class BatteryReceiver extends Broadc AstReceiver {Map <String, Object> map; private Message message; @ Overridepublic void onReceive (Context context, Intent intent) {String action = intent. getAction (); if (action. equals (Intent. ACTION_BATTERY_CHANGED) {map = new HashMap <String, Object> ();/** remaining battery power */map. put ("level", "" + intent. getIntExtra ("level", 0);/** obtain the battery value */map. put ("scale", "" + intent. getIntExtra ("scale", 0);/** obtain battery Technical Support */m Ap. put ("technology", "" + intent. getStringExtra ("technology");/** get Battery status */map. put ("status", "" + intent. getIntExtra ("status", BatteryManager. BATTERY_STATUS_UNKNOWN);/** obtain power information */map. put ("plugType", "" + intent. getIntExtra ("plugged", 0);/** get battery Health */map. put ("health", "" + intent. getIntExtra ("health", BatteryManager. BATTERY_HEALTH_UNKNOWN);/** obtain the battery voltage */map. put ("voltage", intent. getIntExtra ("voltage ", 0) +" ");/** get battery temperature */map. put ("temperature", intent. getIntExtra ("temperature", 0) + ""); if (map! = Null) {message = new Message (); message. obj = map; BatteryManagerActivity. handler1.dispatchMessage (message );}}}}
② Increasing battery size through animation. Now the activity code layout is a bit complicated. I will post the source code later. Let's take a look.
Zero Animation 1. instantiate the animation class
animImageView = new AnimImageView();
2. the animation class implements runnble for time-consuming operations
public class AnimImageView implements Runnable {@Overridepublic void run() {BterryAnim();battery_image.postDelayed(animImageView, 1000);}}
This thread is executed after a delay of one second by defining postDelayed
3. Use Matrix to control the charging Animation
Public void BterryAnim () {float [] stateArray = new float [] {0.1f, 0.25f, 0.5f, 0.75f, 1f}; Bitmap bitmap = BitmapFactory. decodeResource (getResources (), R. drawable. battery_full); int height = bitmap. getHeight (); int width = bitmap. getWidth (); // use Matrix to control the charging animation Matrix matrix = new Matrix (); matrix. postScale (stateArray [state], 1.0f); Bitmap newBitmap = Bitmap. createBitmap (bitmap, 0, 0, width, height, matrix, false); state = state + 1; battery_image.setImageBitmap (newBitmap); if (state = 5) {state = 0 ;}}
I. initialize Layout
Private void init () {/** get batterstate object */battery_Status = (TextView) findViewById (R. id. battery_status);/** get battery running status */battery_Run_Status = (TextView) findViewById (R. id. battery_health);/** obtain the battery voltage */battery_Voltage = (TextView) findViewById (R. id. battery_voltage);/** obtain the battery temperature */battery_Temperature = (TextView) findViewById (R. id. battery_temperature);/** obtain battery technology */battery_Technology = (TextView) findViewById (R. id. battery_technology); // ** back to battery running time * // battery_Time = (TextView) findViewById (R. id. boot_time);/** get battery object */battery_Level = (TextView) findViewById (R. id. battery);/** get the charging icon */bty_Charging = (ImageView) findViewById (R. id. charging);/** get charged */battery_image = (ImageView) findViewById (R. id. battery_image );}
In fact, I am still registering an intent in the code, a broadcast class.
/*** Register broadcast */private void regReceiver () {IntentFilter filter = new IntentFilter (); filter. addAction (Intent. ACTION_BATTERY_CHANGED); registerReceiver (new BatteryReceiver (), filter );}
Then, the broadcast class returns the obtained battery information through the handler class ..........
UI update through handler
/*** Receive BatteryReceiver broadcast class */handler1 = new Handler () {@ Overridepublic void handleMessage (Message msg) {map = (Map <String, Object>) msg. obj; int health = Integer. parseInt (map. get ("health "). toString (); Log. I (TAG, health + ""); if (health = 2) {/** set battery running status */battery_Run_Status.setText ("normal ");} else {/** set battery running status */battery_Run_Status.setText ("general");}/** set battery_Voltage.setText ("" + map. get ("voltage") + "mV");/** set the battery temperature */double time = Double. parseDouble (map. get ("temperature") + ""); battery_Temperature.setText ("" + time/10 + "°C");/** set battery power */max = Double. parseDouble (map. get ("scale") + ""); min = Double. parseDouble (map. get ("level") + ""); temp = (min/max);/** set battery technology */battery_policy.settext ("" + map. get ("technology");/** set startup time * // handler2 = new Handler () {// @ Override // public void handleMessage (Message msg) {// battery_Time.setText ("" + msg. obj. toString (); //};/** set the battery status to USB charging */if (map. get ("plugType "). equals ("2") {// display the charging picture bty_Charging.setVisibility (View. VISIBLE); // The binding display Charging mode is USBbattery_Status.setText ("charging (USB)"); if (isAnimRun = false) {// set the charging animation battery_image.postDelayed (animImageView, 1000); isAnimRun = true;}/** set the battery status to AC charging */} else if (map. get ("plugType "). equals ("1") {// display the charging picture bty_Charging.setVisibility (View. VISIBLE); // The binding display charging method is AC battery_Status.setText ("charging (AC)"); if (isAnimRun = false) {// set the charging animation battery_image.postDelayed (animImageView, 1000); isAnimRun = true;}/** set Battery status */} else if (map. get ("plugType "). equals ("0") {// hide the charging icon bty_Charging.setVisibility (View. INVISIBLE); // set the battery_Status.setText ("power consumption in progress"); if (isAnimRun = true) {// end the charging animation battery_image.removeCallbacks (animImageView ); // set the remaining battery setSize (temp * 100); // disable isAnimRun = false;} setSize (temp * 100); System. out. println (temp + ":" + temp * 100); battery_Level.setText (temp * 100 + "% ");}};}
Click to download source code