ANDROID_ (automated) automatically gets the remaining power of the phone's battery

Source: Internet
Author: User

Automatically get the remaining power of your phone's battery

By using the Broadcastreceiver feature to get the battery charge of the phone, register the Intentfilter set when Broadcastreceiver to get the intent.action_battery_changed from the system, Then use this to get the battery power.

Run:

Program Structure

 Packagecom.example.asus.gary_040a;ImportAndroid.app.Dialog;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;ImportAndroid.content.DialogInterface;Importandroid.content.Intent;ImportAndroid.content.IntentFilter;ImportAndroid.support.v7.app.AlertDialog;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.Window;ImportAndroid.view.WindowManager;ImportAndroid.widget.Button;ImportAndroid.widget.TextView;ImportOrg.w3c.dom.Text; Public classMainactivityextendsappcompatactivity {Private intIntlevel; Private intIntscale; PrivateButton MButton01; PrivateTextView TV; //Create Broadcastreceiver    PrivateBroadcastreceiver Mbatinforeveiver =NewBroadcastreceiver () {@Override Public voidOnReceive (Context context, Intent Intent) {String action=intent.getaction (); //If the captured action is action_battery_changed then run Onbatteryinforeceiver ()            if(Intent. Action_battery_changed.equals (Action)) {//Get current PowerIntlevel = Intent.getintextra ("level", 0); //get the total power of your phoneIntscale = Intent.getintextra ("scale", 100); //This function is defined below to show the current charge of the phoneonbatteryinforeceiver (Intlevel, Intscale);    }        }    }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); MButton01=(Button) Findviewbyid (R.id.mybutton1); Mbutton01.setonclicklistener (NewButton.onclicklistener () {@Override Public voidOnClick (View v) {//register a broadcastreceiver as an access to battery meteringRegisterreceiver (Mbatinforeveiver,NewIntentfilter (intent.action_battery_changed));    }        }); }    //actions to be performed after interception to action_battry_changed    Private voidOnbatteryinforeceiver (intIntlevel,intIntscale) {        //TODO auto-generated Method Stub         intPercent = Intlevel*100/Intscale; //The person you get is the percentage charge.//the percent, not multiplied by 100, is 0.tv=(TextView) Findviewbyid (R.ID.MYTEXTVIEW02); Tv.settext ("The current charge is" +percent+ "%. "); };}
mainactivity

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Vertical"Tools:context= "com.example.asus.gary_040a." Mainactivity "> <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Gary_ automatically gets the remaining battery power of the phone battery!"android:textsize= "40px"App:layout_constraintbottom_tobottomof= "Parent"App:layout_constraintleft_toleftof= "Parent"App:layout_constraintright_torightof= "Parent"App:layout_constrainttop_totopof= "Parent"/> <Button Android:id= "@+id/mybutton1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "button"/> <TextView Android:id= "@+id/mytextview02"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:textsize= "50px"/></linearlayout>
Acvivity_main

First, the interface layout

A button, a TextView text box

When you click on the button, the indicator will appear on the TextView (phone charge)

II. Realization of program function

1. If the ACTION you are capturing is action_battery_changed , run Onbatteryinforeceiver () to display the current phone charge

 PrivateBroadcastreceiver Mbatinforeveiver =NewBroadcastreceiver () {@Override Public voidOnReceive (Context context, Intent Intent) {String action=intent.getaction (); //If the captured action is action_battery_changed then run Onbatteryinforeceiver ()            if(Intent. Action_battery_changed.equals (Action)) {//Get current PowerIntlevel = Intent.getintextra ("level", 0); //get the total power of your phoneIntscale = Intent.getintextra ("scale", 100); //This function is defined below to show the current charge of the phoneonbatteryinforeceiver (Intlevel, Intscale); }        }    };

2. Add Click event Response Actions to button buttons, register system Broadcastreceiver broadcast events to access battery charge

protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); MButton01=(Button) Findviewbyid (R.id.mybutton1); Mbutton01.setonclicklistener (NewButton.onclicklistener () {@Override Public voidOnClick (View v) {//register a broadcastreceiver as an access to battery meteringRegisterreceiver (Mbatinforeveiver,NewIntentfilter (intent.action_battery_changed));    }        }); }

3, the Definition Method Onbatteryinforeceiver (), through this method can display the phone's remaining power on the TextView text box

// Actions to be performed after interception to action_battry_changed    Private void onbatteryinforeceiver (intint  intscale) {        //  TODO auto-generated method stub         int percent = intlevel*100/ Intscale;         // The person you get is the        percentage charge. // the percent, not multiplied by 100, is 0.        tv=(TextView) Findviewbyid (r.id.mytextview02);        Tv.settext ("The current charge is" +percent+ "%.) ");    };

Android_ (automated) automatically gets the remaining battery power of your phone's battery

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.