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相關內容感興趣的讀者可查看本站專題:《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)聽
相關文章
RecyclerView進階:使用ItemTouchHelper實現(xiàn)拖拽和側滑刪除效果
現(xiàn)在RecyclerView的應用越來越廣泛了,本篇文章主要介紹了RecyclerView進階:使用ItemTouchHelper實現(xiàn)拖拽和側滑刪除效果,具有一定的參考價值,有興趣的可以了解一下。2017-02-02Android ListView中動態(tài)添加RaidoButton的實例詳解
這篇文章主要介紹了Android ListView中動態(tài)添加RaidoButton的實例詳解的相關資料,需要的朋友可以參考下2017-08-08