Android電池電量監(jiān)聽的示例代碼
監(jiān)聽電池狀態(tài)只需要接收Intent.ACTION_BATTERY_CHANGED的廣播即可,當(dāng)電池狀態(tài)發(fā)生變化時會發(fā)出廣播。
1.運行狀態(tài)如下圖:
1.充電中的狀態(tài)
2.未充電時的狀態(tài)
2.實現(xiàn)代碼如下,各個狀態(tài)通過名字就很容易知道意思,BatteryManager類中定義了電池狀態(tài)。
public class MainActivity extends Activity { private static final String TAG = "MainActivity"; private TextView mTvVoltage; private TextView mTvTemperature; private TextView mTvLevel; private TextView mTvStatus; private TextView mTvHealth; private TextView mTvTechnology; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTvVoltage = (TextView)findViewById(R.id.tv_voltage); mTvTemperature = (TextView)findViewById(R.id.tv_temperature); mTvLevel = (TextView)findViewById(R.id.tv_level); mTvStatus = (TextView)findViewById(R.id.tv_status); mTvHealth = (TextView)findViewById(R.id.tv_health); mTvTechnology = (TextView)findViewById(R.id.tv_technology); this.registerReceiver(this.mBatteryReceiver, new IntentFilter( Intent.ACTION_BATTERY_CHANGED)); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } private BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() { @Override public void onReceive(Context arg0, Intent arg1) { int voltage=arg1.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0); mTvVoltage.setText("電壓:" + voltage / 1000 + "." + voltage % 1000 + "V"); int temperature=arg1.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0); mTvTemperature.setText("溫度:" + temperature / 10 + "." + temperature % 10 + "℃"); if (temperature >= 300) { mTvTemperature.setTextColor(Color.RED); } else { mTvTemperature.setTextColor(Color.BLUE); } int level=arg1.getIntExtra(BatteryManager.EXTRA_LEVEL,0); int scale=arg1.getIntExtra(BatteryManager.EXTRA_SCALE,0); int levelPercent = (int)(((float)level / scale) * 100); mTvLevel.setText("電量:" + levelPercent + "%"); if (level <= 10) { mTvLevel.setTextColor(Color.RED); } else { mTvLevel.setTextColor(Color.BLUE); } int status = arg1.getIntExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN); String strStatus = "未知狀態(tài)";; switch (status) { case BatteryManager.BATTERY_STATUS_CHARGING: strStatus = "充電中……"; break; case BatteryManager.BATTERY_STATUS_DISCHARGING: strStatus = "放電中……"; break; case BatteryManager.BATTERY_STATUS_NOT_CHARGING: strStatus = "未充電"; break; case BatteryManager.BATTERY_STATUS_FULL: strStatus = "充電完成"; break; } mTvStatus.setText("狀態(tài):" + strStatus); int health = arg1.getIntExtra(BatteryManager.EXTRA_HEALTH, BatteryManager.BATTERY_HEALTH_UNKNOWN); String strHealth = "未知 :(";; switch (status) { case BatteryManager.BATTERY_HEALTH_GOOD: strHealth = "好 :)"; break; case BatteryManager.BATTERY_HEALTH_OVERHEAT: strHealth = "過熱!"; break; case BatteryManager.BATTERY_HEALTH_DEAD: // 未充電時就會顯示此狀態(tài),這是什么鬼? strHealth = "良好"; break; case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE: strHealth = "電壓過高!"; break; case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE: strHealth = "未知 :("; break; case BatteryManager.BATTERY_HEALTH_COLD: strHealth = "過冷!"; break; } mTvHealth.setText("健康狀況:" + strHealth); String technology = arg1.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY); mTvTechnology.setText("電池技術(shù):" + technology); } }; }
3.Layout布局如下,很簡單只有幾個TextView:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/tv_battery_status" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#0000FF" android:textStyle="bold" android:text="@string/battery_status" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_below="@id/tv_battery_status" > <TextView android:id="@+id/tv_voltage" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_temperature" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_level" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_status" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_health" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_technology" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </RelativeLayout>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義view實現(xiàn)動態(tài)柱狀圖
這篇文章主要為大家詳細(xì)介紹了Android自定義view實現(xiàn)動態(tài)柱狀圖的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08Android下拉刷新控件PullToRefresh實例解析
這篇文章主要為大家詳細(xì)解析了Android下拉刷新控件PullToRefresh實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09Android自定義TimeButton實現(xiàn)倒計時按鈕
這篇文章主要為大家詳細(xì)介紹了Android自定義TimeButton實現(xiàn)倒計時按鈕,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-12-12Android百度地圖定位后獲取周邊位置的實現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android百度地圖定位后獲取周邊位置的實現(xiàn)代碼,準(zhǔn)確獲取周邊地理位置,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-01-01Jetpack?Compose?Canvas繪制超詳細(xì)介紹
Canvas?是允許您在屏幕上指定區(qū)域并在此區(qū)域上執(zhí)行繪制的組件。您必須使用修飾符指定尺寸,無論是通過Modifier.size修飾符指定確切尺寸,還是通過Modifier.fillMaxSize,ColumnScope.weight等相對于父級指定精確尺寸。如果父級包裝了此子級,則僅必須指定確切尺寸2022-10-10Android視頻/音頻緩存框架AndroidVideoCache(Okhttp)詳解
這篇文章主要為大家詳細(xì)介紹了Android視頻、音頻緩存框架AndroidVideoCache,實現(xiàn)邊下邊播功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07Android實現(xiàn)可瀏覽和搜索的聯(lián)系人列表
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)可瀏覽和搜索的聯(lián)系人列表的相關(guān)代碼,瀏覽所有聯(lián)系人和根據(jù)名稱搜索聯(lián)系人,感興趣的小伙伴們可以參考一下2016-07-07Android下Activity全屏顯示實現(xiàn)方法
這篇文章主要介紹了Android下Activity全屏顯示實現(xiàn)方法,以兩種不同的方法來實現(xiàn)這一技巧,非常具有實用性,需要的朋友可以參考下2014-10-10