Monitoring of cell phone power changes when receiving system broadcast messages

Source: Internet
Author: User

When the cell phone power changes, the system will send the Intent Action to android. intent. action. BATTERY_CHANGED constant broadcast; when the cell phone power is too low, the system will send Intent Action as android. intent. action. broadcast of the BATTERY_LOW constant.

When the battery status of the mobile phone is restored from insufficient power, the system will send a broadcast that the Intent Action is android. intent. action. BATTERY_OKAY constant.

The following is a simple example:

Aggreger:


 

Package com. home. explorer; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. OS. bundle; import android. widget. toast; public class BatteryReceiver extends BroadcastReceiver {@ Override public void onReceive (Context context, Intent intent) {if (Intent. ACTION_BATTERY_OKAY.equals (intent. getAction () {Toast. makeText (context, "power restored, Available! ", Toast. LENGTH_LONG). show () ;}if (Intent. ACTION_BATTERY_LOW.equals (intent. getAction () {Toast. makeText (context," the power is too low. Please charge it as soon as possible! ", Toast. LENGTH_LONG ). show ();} if (Intent. ACTION_BATTERY_CHANGED.equals (intent. getAction () {Bundle bundle = intent. getExtras (); // get the current power int current = bundle. getInt ("level"); // get the total power int total = bundle. getInt ("scale"); StringBuffer sb = new StringBuffer (); sb. append ("current power:" + current * 100/total + "%" + ""); // if the current power is less than 15% of the total power, if (current * 1.0/total <0.15) {sb. append ("power is too low, please charge as soon as possible! ");} Else {sb. append (" enough power, please feel free to use it! ");} Toast. makeText (context, sb. toString (), Toast. LENGTH_LONG ). show () ;}} package com. home. explorer; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. OS. bundle; import android. widget. toast; public class BatteryReceiver extends BroadcastReceiver {@ Override public void onReceive (Context context, Intent intent) {if (Intent. ACTION_ B ATTERY_OKAY.equals (intent. getAction () {Toast. makeText (context, "the power has been restored and can be used! ", Toast. LENGTH_LONG). show () ;}if (Intent. ACTION_BATTERY_LOW.equals (intent. getAction () {Toast. makeText (context," the power is too low. Please charge it as soon as possible! ", Toast. LENGTH_LONG ). show ();} if (Intent. ACTION_BATTERY_CHANGED.equals (intent. getAction () {Bundle bundle = intent. getExtras (); // get the current power int current = bundle. getInt ("level"); // get the total power int total = bundle. getInt ("scale"); StringBuffer sb = new StringBuffer (); sb. append ("current power:" + current * 100/total + "%" + ""); // if the current power is less than 15% of the total power, if (current * 1.0/total <0.15) {sb. append ("power is too low, please charge as soon as possible! ");} Else {sb. append (" enough power, please feel free to use it! ") ;}Toast. makeText (context, sb. toString (), Toast. LENGTH_LONG). show ();}}}

Configure aggreger:

receiver android:name="com.home.receiver.BatteryReceiver">           <intent-filter>               <action android:name="android.intent.action.BATTERY_CHANGED" />               <action android:name="android.intent.action.BATTERY_OKAY"/>               <action android:name="android.intent.action.BATTERY_LOW"/>           </intent-filter>         </receiver>  <receiver android:name="com.home.receiver.BatteryReceiver">           <intent-filter>               <action android:name="android.intent.action.BATTERY_CHANGED" />               <action android:name="android.intent.action.BATTERY_OKAY"/>               <action android:name="android.intent.action.BATTERY_LOW"/>           </intent-filter>        </receiver> 

 

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.