Home | History | Annotate | Download | only in monitoring-device-state
      1 page.title=
      2 parent.title=
      3 parent.link=index.html
      4 
      5 trainingnavtop=true
      6 next.title=
      7 next.link=docking-monitoring.html
      8 
      9 @jd:body
     10  
     11 <div id="tb-wrapper"> 
     12 <div id="tb">
     13 
     14 <h2></h2>
     15 <ol>
     16   <li><a href="#DetermineChargeState"></a></li>
     17   <li><a href="#MonitorChargeState"></a></li>
     18   <li><a href="#CurrentLevel"></a></li>
     19   <li><a href="#MonitorLevel"></a></li>
     20 </ol>
     21 
     22 <h2></h2>
     23 <ul>
     24   <li><a href="{@docRoot}guide/components/intents-filters.html">intent  intent </a>
     25 </ul>
     26 
     27 </div> 
     28 </div>
     29  
     30 <p></p>
     31 
     32 <p></p>
     33 
     34 <p></p>
     35 
     36 
     37 <h2 id="DetermineChargeState"></h2> 
     38  
     39 <p>{@link android.os.BatteryManager}  {@link android.content.Intent} </p>
     40 
     41 <p> intent {@code null}  {@code registerReceiver}  {@link android.content.BroadcastReceiver} intent {@link android.content.BroadcastReceiver} </p>
     42 
     43 <pre>IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
     44 Intent batteryStatus = context.registerReceiver(null, ifilter);</pre>
     45 
     46 <p> USB <p>
     47 
     48 <pre>// Are we charging / charged?
     49 int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
     50 boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
     51                      status == BatteryManager.BATTERY_STATUS_FULL;
     52 
     53 // How are we charging?
     54 int chargePlug = battery.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
     55 boolean usbCharge = chargePlug == BATTERY_PLUGGED_USB;
     56 boolean acCharge = chargePlug == BATTERY_PLUGGED_AC;</pre>
     57 
     58 <p> USB </p>
     59 
     60 
     61 <h2 id="MonitorChargeState"></h2> 
     62 
     63 <p></p>
     64 
     65 <p>{@link android.os.BatteryManager}  intent  {@link android.content.Intent#ACTION_POWER_CONNECTED}  {@link android.content.Intent#ACTION_POWER_DISCONNECTED} {@link android.content.BroadcastReceiver} </p>
     66 
     67 <pre>&lt;receiver android:name=".PowerConnectionReceiver">
     68   &lt;intent-filter>
     69     &lt;action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
     70     &lt;action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
     71   &lt;/intent-filter>
     72 &lt;/receiver></pre>
     73 
     74 <p> {@link android.content.BroadcastReceiver} </p>
     75 
     76 <pre>public class PowerConnectionReceiver extends BroadcastReceiver {
     77     &#64;Override
     78     public void onReceive(Context context, Intent intent) { 
     79         int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
     80         boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
     81                             status == BatteryManager.BATTERY_STATUS_FULL;
     82     
     83         int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
     84         boolean usbCharge = chargePlug == BATTERY_PLUGGED_USB;
     85         boolean acCharge = chargePlug == BATTERY_PLUGGED_AC;
     86     }
     87 }</pre>
     88 
     89 
     90 <h2 id="CurrentLevel"></h2> 
     91 
     92 <p></p>
     93 
     94 <p> intent </p>
     95 
     96 <pre>int level = battery.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
     97 int scale = battery.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
     98 
     99 float batteryPct = level / (float)scale;</pre>
    100 
    101 
    102 <h2 id="MonitorLevel"></h2> 
    103 
    104 <p></p>
    105 
    106 <p></p>
    107 
    108 <p> intent  {@link android.content.Intent#ACTION_BATTERY_LOW}  {@link android.content.Intent#ACTION_BATTERY_OKAY}</p>
    109 
    110 <pre>&lt;receiver android:name=".BatteryLevelReceiver">
    111 &lt;intent-filter>
    112   &lt;action android:name="android.intent.action.ACTION_BATTERY_LOW"/>
    113   &lt;action android:name="android.intent.action.ACTION_BATTERY_OKAY"/>
    114   &lt;/intent-filter>
    115 &lt;/receiver></pre>
    116 
    117 <p></p>
    118 
    119 <p></p>
    120 
    121