Android查看電池電量的方法(基于BroadcastReceiver)
本文實例講述了Android查看電池電量的方法。分享給大家供大家參考,具體如下:
程序如下:
import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
public class A02Activity extends Activity {
private int level;
private int scale;
private Button b01;
private BroadcastReceiver mBatInfoReceiver=new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action=intent.getAction();
//如果捕捉到的是ACTION_BATTERY_CHANGED就運行onBatteryInfoReceiver();將電量顯示于新窗口中
if(Intent.ACTION_BATTERY_CHANGED.equals(action)){
level=intent.getIntExtra("level", 0);
scale=intent.getIntExtra("scale", 100);
onBatteryInfoReceiver(level,scale);
}
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b01=(Button)findViewById(R.id.button01);
b01.setBackgroundColor(Color.GREEN);
b01.setText("查看電量");
b01.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
registerReceiver(mBatInfoReceiver,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
});
}
public void onBatteryInfoReceiver(int intLevel,int intScale){
final Dialog d=new Dialog(A02Activity.this);
d.setTitle(R.string.str_title);
d.setContentView(R.layout.dialog);
Window window=d.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
TextView mTextView02=(TextView)d.findViewById(R.id.myTextView02);
//取得電池電量顯示于Dialog中
mTextView02.setText(getResources().getText(R.string.str_body)+String.valueOf(intLevel*100/intScale)+"%");
Button b02=(Button)d.findViewById(R.id.button02);
b02.setBackgroundColor(Color.RED);
b02.setText("返回");
b02.setTextColor(Color.YELLOW);
b02.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// 反注冊Receiver并關閉窗口
unregisterReceiver(mBatInfoReceiver);
d.dismiss();
}
});
d.show();
}
}
在Android中,Android.intent.BATTERY_CHANGED是系統(tǒng)的Broadcast Action Message,當電池處于充電狀態(tài)或電池電量有變化時,系統(tǒng)便會廣播此Action;程序中的BroadcastReceiver在注冊時,由于設置了Intent Filter過濾此Action信息,因此當BroadcastReceiver一被注冊,就能馬上捕捉這個Action,進而取得電池電量。
主程序中的onReceiver()是當BroadcastReceiver被觸發(fā)時會運行的方法,寫法如下:
public void onReceiver(Context context,Intent intent){
String action=intent.getAction();
if(Intent.ACTION_BATTERY_CHANGED.equals(action)){
/*運行程序的代碼*/
}
}
添加這一判斷Intent.ACTION_BATTERY_CHANGED.equals(action)是為了確保BroadcastReceiver只會被Intent.ACTION_BATTERY_CHANGED這個觸發(fā)。如果沒有這個判斷程序也是可以運行的。
Android API中說明,要注冊含有Intent.ACTION_BATTERY_CHANGED的Receiver,只能在程序中以Context.registerReceiver()方式來注冊,不能直接在AndroidManifest.xml中注冊。
本例中使用了讓Dialog在彈出時,背景的窗口呈現(xiàn)模糊的狀態(tài):
final Dialog d=new Dialog(A02Activity.this); d.setTitle(R.string.str_title); d.setContentView(R.layout.dialog); Window window=d.getWindow(); window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
其中WindowManager.LayoutParams.FLAG_BLUR_BEHIND是告訴目前的Window不管是什么對象顯示于前端,都會出現(xiàn)在Window的最上層,讓背景Window呈現(xiàn)模糊狀態(tài)。也可以在其他程序中使用這個效果。
更多關于Android相關內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結》及《Android開發(fā)入門與進階教程》
希望本文所述對大家Android程序設計有所幫助。
- 深入Android中BroadcastReceiver的兩種注冊方式(靜態(tài)和動態(tài))詳解
- Android中BroadcastReceiver(異步接收廣播Intent)的使用
- Android提高之BroadcastReceiver實例詳解
- Android編程四大組件之BroadcastReceiver(廣播接收者)用法實例
- Android BroadcastReceiver廣播注冊方式總結
- Android開發(fā)之BroadcastReceiver用法實例分析
- 詳解Android中BroadCastReceiver組件
- Android采取BroadcastReceiver方式自動獲取驗證碼
- Android BroadcastReceiver常見監(jiān)聽整理
- Android BroadcastReceiver實現(xiàn)網(wǎng)絡狀態(tài)實時監(jiān)聽
相關文章
快速解決android webview https圖片不顯示的問題
今天小編就為大家分享一篇快速解決android webview https圖片不顯示的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
Studio 編譯報錯:compileSdkVersion ''android-24'' requires JDK 1.
今天小編就為大家分享一篇關于Studio編譯報錯:compileSdkVersion 'android-24' requires JDK 1.8 or later to compile.的解決辦法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10

