BSP development for MSM8909 + Android5.1.1 --- BatteryInfo. java

Source: Internet
Author: User

BSP development for MSM8909 + Android5.1.1 --- BatteryInfo. java

BSP development for MSM8909 + Android5.1.1 --- BatteryInfo. java

 

Use MTK's battery management framework

 

Figure 1

 

Run the telephone test command: * #4636 # *. The Testing interface is displayed.

 

Figure 2

Select Battery information to go:

 

Figure 3

When we connect to USB or DC for charging, Unknown is displayed when Power plug is used.

It indicates that the underlying layer is determined to be of the Unknown type. This issue should be clearly identified.

 

The code for BatteryInfo is packages \ apps \ Settings \ src \ com \ android \ settings \ BatteryInfo. java.

 

1. \ packages \ apps \ Settings \ AndroidManifest. xml

 

           
                                                            
        

 

(1) android: label

Android: label = "@ string/battery_info_label", label indicates a label, @ indicates a reference, indicating that the value of battery_info_label is referenced from the string file,

The values in \ packages \ apps \ Settings \ res \ values \ strings. xml are as follows:

Battery info

 

(2) android: taskAffinity

Android: taskAffinity = "com. android. settings" indicates that the BatteryInfo Activity wants to enter the task com. android. settings.

 

The ownership of the Activity, that is, the adsorption relationship between the Activity and the Task in which the Activity should be located. We know that in general, in the same application, all started activities are in the same Task, and they spend their own lifecycles in the Task. These activities are a good example for the end-to-end.

 

So Why will the Activity we created enter this Task? Will they be transferred to other tasks? If it is transferred to another Task, what Task will it go?

 

The key to solving these problems lies in the taskAffinity attribute of each Activity.

 

Each Activity has the taskAffinity attribute, which specifies the Task it wants to enter. If an Activity does not explicitly specify the taskAffinity of the Activity, its attribute is equal to the taskAffinity specified by the Application. If the Application does not, the value of taskAffinity is equal to the package name. Task also has its own affinity attribute. Its value is equal to the taskAffinity value of its root Activity.

 

2. BatteryInfo. java code analysis

 

2.1 onCreate ()

 

@Override   public void onCreate(Bundle icicle) {       super.onCreate(icicle);        setContentView(R.layout.battery_info);        // create the IntentFilter that will be used to listen       // to battery status broadcasts       mIntentFilter = new IntentFilter();       mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);    }

 

(1) R. layout. battery_info

Its value is defined in packages \ apps \ Settings \ res \ layout \ battery_info.xml.

(2) create an IntentFilter Class Object mIntentFilter

Monitors Battery status broadcasts.

 

(3) mIntentFilter. addAction (Intent. ACTION_BATTERY_CHANGED );

Add an ACTION_BATTERY_CHANGED action to IntentFilter, that is, when other activities or services send intent containing ACTION_BATTERY_CHANGED, BatteryInfo updates the batterystate.

 

2.2 onResume ()

 

public void onResume() {       super.onResume();        mStatus = (TextView)findViewById(R.id.status);       mPower = (TextView)findViewById(R.id.power);       mLevel = (TextView)findViewById(R.id.level);       mScale = (TextView)findViewById(R.id.scale);       mHealth = (TextView)findViewById(R.id.health);       mTechnology = (TextView)findViewById(R.id.technology);       mVoltage = (TextView)findViewById(R.id.voltage);       mTemperature = (TextView)findViewById(R.id.temperature);       mUptime = (TextView) findViewById(R.id.uptime);              // Get awake time plugged in and on battery       mBatteryStats =IBatteryStats.Stub.asInterface(ServiceManager.getService(                BatteryStats.SERVICE_NAME));       mScreenStats = IPowerManager.Stub.asInterface(ServiceManager.getService(POWER_SERVICE));       mHandler.sendEmptyMessageDelayed(EVENT_TICK, 3000);              registerReceiver(mIntentReceiver, mIntentFilter);    }

 

In the onResume () method, use registerReceiver (mIntentReceiver, mIntentFilter); to register a Receiver, and finally receive an action with the value of ACTION_BATTERY_CHANGED in the onReceive () method, the BatteryInfo Activity updates the batterystatus information.

 

(1) create a Handler

MHandler. sendEmptyMessageDelayed (EVENT_TICK, 3000); When Handler is used, it is created as follows:

 

private Handler mHandler = new Handler() {        @Override       public void handleMessage(Message msg) {           switch (msg.what) {                case EVENT_TICK:                    updateBatteryStats();                   sendEmptyMessageDelayed(EVENT_TICK, 1000);                                       break;           }       }};

 

Override handleMessage () to process the message EVENT_TICK. After receiving this message, call updateBatteryStats () to update the battery status, and send the EVENT_TICK message after Ms.

 

(2) register a listener

 

/**   *Listens for intent broadcasts   */   private IntentFilter  mIntentFilter;    private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {       @Override       public void onReceive(Context context, Intent intent) {           String action = intent.getAction();           if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {                int plugType =intent.getIntExtra("plugged", 0);                 mLevel.setText("" +intent.getIntExtra("level", 0));                mScale.setText("" +intent.getIntExtra("scale", 0));                mVoltage.setText("" +intent.getIntExtra("voltage", 0) + " "                        +getString(R.string.battery_info_voltage_units));               mTemperature.setText("" +tenthsToFixedString(intent.getIntExtra("temperature", 0))                        +getString(R.string.battery_info_temperature_units));               mTechnology.setText("" +intent.getStringExtra("technology"));                              mStatus.setText(Utils.getBatteryStatus(getResources(), intent));                              switch (plugType) {                    case 0:                       mPower.setText(getString(R.string.battery_info_power_unplugged));                        break;                    case BatteryManager.BATTERY_PLUGGED_AC:                       mPower.setText(getString(R.string.battery_info_power_ac));                        break;                    case BatteryManager.BATTERY_PLUGGED_USB:                       mPower.setText(getString(R.string.battery_info_power_usb));                        break;                    caseBatteryManager.BATTERY_PLUGGED_WIRELESS:                       mPower.setText(getString(R.string.battery_info_power_wireless));                        break;                    case(BatteryManager.BATTERY_PLUGGED_AC|BatteryManager.BATTERY_PLUGGED_USB):                       mPower.setText(getString(R.string.battery_info_power_ac_usb));                        break;                    default:                        mPower.setText(getString(R.string.battery_info_power_unknown));                        break;                }                               int health =intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN);                String healthString;                if (health ==BatteryManager.BATTERY_HEALTH_GOOD) {                    healthString =getString(R.string.battery_info_health_good);                } else if (health ==BatteryManager.BATTERY_HEALTH_OVERHEAT) {                   healthString =getString(R.string.battery_info_health_overheat);                } else if (health ==BatteryManager.BATTERY_HEALTH_DEAD) {                    healthString =getString(R.string.battery_info_health_dead);                } else if (health ==BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE) {                    healthString =getString(R.string.battery_info_health_over_voltage);                } else if (health ==BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE) {                    healthString =getString(R.string.battery_info_health_unspecified_failure);                } else if (health ==BatteryManager.BATTERY_HEALTH_COLD) {                    healthString =getString(R.string.battery_info_health_cold);                } else {                    healthString =getString(R.string.battery_info_health_unknown);                }                mHealth.setText(healthString);           }       }   };

 

1) Update Battery status information

Here, the Battery status is special:

MStatus. setText (Utils. getBatteryStatus (getResources (), intent ));

See packages \ apps \ Settings \ src \ com \ android \ settings \ Utils. java \ getBatteryStatus () Implementation:

 

public static StringgetBatteryStatus(Resources res, Intent batteryChangedIntent) {       final Intent intent = batteryChangedIntent;        int plugType = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);       int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,               BatteryManager.BATTERY_STATUS_UNKNOWN);       String statusString;       if (status == BatteryManager.BATTERY_STATUS_CHARGING) {           int resId;           if (plugType == BatteryManager.BATTERY_PLUGGED_AC) {                resId =R.string.battery_info_status_charging_ac;           } else if (plugType == BatteryManager.BATTERY_PLUGGED_USB) {                resId =R.string.battery_info_status_charging_usb;           } else if (plugType == BatteryManager.BATTERY_PLUGGED_WIRELESS) {                resId =R.string.battery_info_status_charging_wireless;           } else {                resId =R.string.battery_info_status_charging;           }           statusString = res.getString(resId);        } else if (status ==BatteryManager.BATTERY_STATUS_DISCHARGING) {           statusString = res.getString(R.string.battery_info_status_discharging);       } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {           statusString = res.getString(R.string.battery_info_status_not_charging);       } else if (status == BatteryManager.BATTERY_STATUS_FULL) {           statusString = res.getString(R.string.battery_info_status_full);       } else {           statusString = res.getString(R.string.battery_info_status_unknown);       }        return statusString;}

 


As shown above, the plugType is unknown, so the charging status is Charing from battery_info_status_charging of \ packages \ apps \ Settings \ res \ values \ strings. xml, as shown below:

Charging

 

 

2) how to obtain battery Information

 

public void onReceive(Context context,Intent intent) {           String action = intent.getAction();           if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {                int plugType =intent.getIntExtra("plugged", 0);


 

This is a piece of code above. When you receive the broadcast, and the intent action value is ACTION_BATTERY_CHANGED, the value of plugged is read from the intent, which is broadcast by BatteryService.

 

2.3 onPause ()

 

@Override   public void onPause() {       super.onPause();       mHandler.removeMessages(EVENT_TICK);       Slog.e(TAG, "BatteryInfo--->onPause()");       // we are no longer on the screen stop the observers       unregisterReceiver(mIntentReceiver);}

 

It is relatively simple, that is, deleting EVENT_TICK messages and logging out of the listener mIntentReceiver

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.