今天的文章要來寫寫Android中如何抓取電池資訊的方法(BatteryManager)
電池狀態一般APP比較少,但多適用於一些顯示手機狀態的APP較常使用
但是不管,我們本來就是要什麼功能都寫的麻!哇哈哈
那來看一下成果圖(我懶...直接放靜態圖...)
這是跑手機模擬器的,所以都會是固定數值
跑我實體手機結果如下
那就,讓我們來看看程式吧
1. 理論
首先介面只有兩個TextView,這部分怎麼設置應該不需要我介紹了吧?
那麼試問,這項功能到底是如何被實踐的呢?
答案是廣播(Broadcast)
我在以前的文章也有寫過廣播,大家可以去看看我到底寫了些什麼
->碼農日常-『Android studio』Broadcast廣播的應用方法
此外,在本範例中,我們要取得的電池資訊,則皆可從BatteryManager中過濾出來
像這次我就過濾出電池狀態、電池健康、溫度等等
其實不會很難!看程式碼吧
2. 撰寫廣播
好,那首先先寫廣播的部分
想當然爾,創立一個廣播
public class MainActivity extends AppCompatActivity { public static final String TAG = MainActivity.class.getSimpleName()+"My"; TextView tvResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvResult = findViewById(R.id.tvResult); } private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); } }; }
再來,建立Filter
public class MainActivity extends AppCompatActivity { public static final String TAG = MainActivity.class.getSimpleName()+"My"; TextView tvResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvResult = findViewById(R.id.tvResult); IntentFilter mIntentFilter = new IntentFilter(); mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); registerReceiver(mIntentReceiver, mIntentFilter); } private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); } }; }
3. 過濾電池資訊
直接看Code最快了XD
public class MainActivity extends AppCompatActivity { public static final String TAG = MainActivity.class.getSimpleName()+"My"; TextView tvResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvResult = findViewById(R.id.tvResult); IntentFilter mIntentFilter = new IntentFilter(); mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); registerReceiver(mIntentReceiver, mIntentFilter); } private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); //從這邊開始 if (action.equals(Intent.ACTION_BATTERY_CHANGED)) { StringBuilder builder = new StringBuilder(); builder.append("電池狀態: "); int status = intent.getIntExtra("status", 0); // 电池状态 switch (status){ case BatteryManager.BATTERY_STATUS_CHARGING: builder.append("充電中"); break; case BatteryManager.BATTERY_STATUS_DISCHARGING: builder.append("放電中"); break; case BatteryManager.BATTERY_STATUS_NOT_CHARGING: builder.append("未充電"); break; case BatteryManager.BATTERY_STATUS_FULL: builder.append("電量已滿"); break; } builder.append("\n"); builder.append("電池健康情形: "); int health = intent.getIntExtra("health", 0); switch (health){ case BatteryManager.BATTERY_HEALTH_GOOD: builder.append("良好"); break; case BatteryManager.BATTERY_HEALTH_OVERHEAT: builder.append("溫度過高"); break; case BatteryManager.BATTERY_HEALTH_DEAD: builder.append("電池已耗盡"); break; case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE: builder.append("過電壓"); break; case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE: builder.append("未知錯誤"); break; } builder.append("\n"); builder.append("電池最大容量: "); int scale = intent.getIntExtra("scale", 0); builder.append(scale+"%"); builder.append("\n"); builder.append("電池電壓: "); int voltage = intent.getIntExtra("voltage", 0); builder.append(voltage+"mV"); builder.append("\n"); builder.append("電池電量: "); int level = intent.getIntExtra("level", 0); builder.append(level+"%"); builder.append("\n"); builder.append("電池溫度: "); int temperature = intent.getIntExtra("temperature", 0); builder.append((Math.round(temperature)/10.0)+"度"); builder.append("\n"); tvResult.setText(builder.toString()); } //到這邊 } }; }
所有的資訊都是從intent過來的,然後再用BatteryManager去做過濾
就~是這麼簡單XD
結語
這週我其實有安排要去面試,目前人正在高鐵站
所以~這篇是直得紀念地難得在外面打的文章!?(誒?
由於這週真的很忙(加上下大雨...
所以原本想要寫的主題就延後惹QQ
老樣子,這篇到這邊,如果文章有幫助..
文章標籤
全站熱搜
留言列表