Monitor the power status of your Android device with a broadcast

Source: Internet
Author: User

 Send Notifications

This email will discuss how to get the Power status on your Android device and we will use the broadcast to accomplish this goal.

What is Broadcastreceiver?
A broadcast receiver is an Android component which allows the register for system or application events. All registered receivers for an event is notified by the Android application.

The system itself broadcast event all time, as example if system gets booted or an SMS arrived, etc.

Application description
Application display of battery level and update this value is battery level changed.

Create a new project in Android Studio


Add a constant in the String.xml

    1. <string name= "Battery_level" >battery level:</string>

        

Update the layout file, here I'm called "Activity_main.xml"

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Xmlns:tools="Http://schemas.android.com/tools"Android:layout_width="match_parent"Android:layout_height="match_parent"Android:paddingleft="@dimen/activity_horizontal_margin"Android:paddingright="@dimen/activity_horizontal_margin"Android:paddingtop="@dimen/activity_vertical_margin"Android:paddingbottom="@dimen/activity_vertical_margin"android:orientation="Vertical"Tools:context=". Mainactivity"> <TextView Android:id="@+id/textview"Android:text="@string/battery_level"Android:layout_width="wrap_content"Android:layout_height="wrap_content"/> <ProgressBar Android:id="@+id/progressbar"style="? android:attr/progressbarstylehorizontal"Android:layout_margintop="8DP"Android:layout_marginleft="12DP"Android:layout_marginright="12DP"Android:max=" -"Android:layout_width="match_parent"Android:layout_height="wrap_content"Android:layout_below="@+id/textview"Android:layout_torightof="@+id/textview"android:layout_toendof="@+id/textview"/></linearlayout>

Create an inner class for our broadcast implementation


Private classBatterybroadcastreceiver extends Broadcastreceiver {PrivateFinalStaticString Battery_level =" Level"; @Override Public voidOnReceive (Context context, Intent Intent) {intLevel = Intent.getintextra (Battery_level,0); Mbatteryleveltext.settext (GetString (R.string. Battery_level) +" "+Level );    Mbatterylevelprogress.setprogress (level); }}

The code to update the activity is as follows:

 Public classMainactivity extends Activity {PrivateTextView Mbatteryleveltext; PrivateProgressBar mbatterylevelprogress; PrivateBroadcastreceiver Mreceiver; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Mbatteryleveltext=(TextView) Findviewbyid (R.id.textview); Mbatterylevelprogress=(ProgressBar) Findviewbyid (R.id.progressbar); Mreceiver=NewBatterybroadcastreceiver (); Registerreceiver (Mreceiver,NewIntentfilter (intent.action_battery_changed)); } @Overrideprotected voidOnStop () {unregisterreceiver (mreceiver);    Super.onstop (); }    Private classBatterybroadcastreceiver extends Broadcastreceiver {PrivateFinalStaticString Battery_level =" Level"; @Override Public voidOnReceive (Context context, Intent Intent) {intLevel = Intent.getintextra (Battery_level,0); Mbatteryleveltext.settext (GetString (R.string. Battery_level) +" "+Level );        Mbatterylevelprogress.setprogress (level); }    }}

We must counter-register your recipient when activity stops, because if you do not unregister, the process will always receive some information in the background when your application shuts down.

To register a broadcast:

inch androidmanifest.xml:<receiver android:name="batterybroadcastreceiver" ><intent-filter><action android:name="android.intent.action.BATTERY_CHANGED" /></intent-filter></receiver>


in Newnew intentfilter (intent.action_battery_changed));


Run the program
We can change battery level of our emulator use console. We must connect to off AVD Emulator use console/terminal. If We run many emulator, each emulator would have a unique port. Port number your emulator you can find in title of Emulator window. As example port of my emulator is 5554.
Connect to Emulator:
    1. telnet localhost <PORT>

Change battery level of emulator:
    1. Power Capacity <BATTERY_LEVEL_IN_PERCENT>


Next step is change battery level on emulator.

As can see TextView and ProgressBar were updated. from:http://alexzh.com/tutorials/android-battery-status-use-broadcastreceiver/?utm_source=android+weekly& utm_campaign=bce41f280c-android_weekly_149&utm_medium=email&utm_term=0_4eb677ad19-bce41f280c-337902977

Monitor the power status of your Android device with a broadcast

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.