获取手机电池的剩余电量
生活随笔
收集整理的這篇文章主要介紹了
获取手机电池的剩余电量
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
取得手機電池的剩余量
?* Android API中的BroadcastReceiver(Android.content.BroadcastReceiver)
?* 類有點像Button中的Listener,當Receiver被注冊后,會在后臺等待其他程序
?* 的調用,程序將通過注冊BroadcastReceiver時設置的IntentFilter來捕捉系統
?* 發出的Intent.ACTION_BATTERY_CHANGED這個action,再以此取得手機電池的剩
?* 余量。 public class Ex06_02Activity extends Activity { private int intLevel; private int intScale; private Button mButton01; private AlertDialog d; // 創建BroadcastReceiver private BroadcastReceiver mBatInfoReveiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); // 如果捕捉到action是ACRION_BATTERY_CHANGED // 就運行onBatteryInfoReveiver() if (intent.ACTION_BATTERY_CHANGED.equals(action)) { intLevel = intent.getIntExtra("level", 0); intScale = intent.getIntExtra("scale", 100); onBatteryInfoReceiver(intLevel, intScale); } } }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mButton01 = (Button) findViewById(R.id.myButton1); mButton01.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 注冊一個BroadcastReceiver,作為訪問電池計量之用 registerReceiver(mBatInfoReveiver, new IntentFilter( Intent.ACTION_BATTERY_CHANGED)); } }); } // 攔截到ACTION_BATTRY_CHANGED后要執行的動作 private void onBatteryInfoReceiver(int intLevel, int intScale) { // TODO Auto-generated method stub d = new AlertDialog.Builder(Ex06_02Activity.this).create(); d.setTitle(R.string.str_dialog_title); d.setMessage(getResources().getString(R.string.str_dialog_body) + String.valueOf(intLevel * 100 / intScale) + "%"); d.setButton(getResources().getString(R.string.str_button2), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub // 取消注冊,并關閉對話框 unregisterReceiver(mBatInfoReveiver); d.dismiss(); } }); d.show(); }; }
if (level <= 20) {try {if (mLight != null)mLightsService.close(mLight.getType());} catch (Exception e) {e.printStackTrace();}if (status == BatteryManager.BATTERY_STATUS_CHARGING) {// Solid red when battery is charging// mBatteryLight.setColor(mBatteryLowARGB);mLight = new com.android.internal.policy.Light(Light.CHARGING, 3000, 5000, mBatteryLowARGB);} else {// Flash red when battery is low and not charging// mBatteryLight.setFlashing(mBatteryLowARGB,// LightsService.LIGHT_FLASH_TIMED,// batteryLowLedOn, batteryLowLedOff);mLight = new com.android.internal.policy.Light(Light.LOW_BATTERY, batteryLowLedOn,batteryLowLedOff, mBatteryLowARGB);}try {mLightsService.open(mLight);} catch (Exception e) {e.printStackTrace();}} else {// mBatteryLight.turnOff();try {if (mLight != null)mLightsService.close(mLight.getType());elsemLightsService.close(com.android.internal.policy.Light.POWER_ALL); //i dont have time to test,set this to avoid the light not off} catch (Exception e) {e.printStackTrace();}if (level < 90) {mLight = new com.android.internal.policy.Light(Light.CHARGING, 3000, 5000, mBatteryMediumARGB);}else if(level >= 90){mLight = new com.android.internal.policy.Light(Light.CHARGE_COMPLETE,3000, 5000,mBatteryFullARGB);}else if (status == BatteryManager.BATTERY_STATUS_FULL){// mBatteryLight.setColor(mBatteryFullARGB);mLight = new com.android.internal.policy.Light(Light.CHARGE_COMPLETE, 0, 0, mBatteryFullARGB);}try {mLightsService.open(mLight);} catch (Exception e) {e.printStackTrace();}}
?* Android API中的BroadcastReceiver(Android.content.BroadcastReceiver)
?* 類有點像Button中的Listener,當Receiver被注冊后,會在后臺等待其他程序
?* 的調用,程序將通過注冊BroadcastReceiver時設置的IntentFilter來捕捉系統
?* 發出的Intent.ACTION_BATTERY_CHANGED這個action,再以此取得手機電池的剩
?* 余量。 public class Ex06_02Activity extends Activity { private int intLevel; private int intScale; private Button mButton01; private AlertDialog d; // 創建BroadcastReceiver private BroadcastReceiver mBatInfoReveiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); // 如果捕捉到action是ACRION_BATTERY_CHANGED // 就運行onBatteryInfoReveiver() if (intent.ACTION_BATTERY_CHANGED.equals(action)) { intLevel = intent.getIntExtra("level", 0); intScale = intent.getIntExtra("scale", 100); onBatteryInfoReceiver(intLevel, intScale); } } }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mButton01 = (Button) findViewById(R.id.myButton1); mButton01.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 注冊一個BroadcastReceiver,作為訪問電池計量之用 registerReceiver(mBatInfoReveiver, new IntentFilter( Intent.ACTION_BATTERY_CHANGED)); } }); } // 攔截到ACTION_BATTRY_CHANGED后要執行的動作 private void onBatteryInfoReceiver(int intLevel, int intScale) { // TODO Auto-generated method stub d = new AlertDialog.Builder(Ex06_02Activity.this).create(); d.setTitle(R.string.str_dialog_title); d.setMessage(getResources().getString(R.string.str_dialog_body) + String.valueOf(intLevel * 100 / intScale) + "%"); d.setButton(getResources().getString(R.string.str_button2), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub // 取消注冊,并關閉對話框 unregisterReceiver(mBatInfoReveiver); d.dismiss(); } }); d.show(); }; }
系統獲取電池方法以及根據電量改變指示燈的顏色,在BatteryService類中修改
if (level <= 20) {try {if (mLight != null)mLightsService.close(mLight.getType());} catch (Exception e) {e.printStackTrace();}if (status == BatteryManager.BATTERY_STATUS_CHARGING) {// Solid red when battery is charging// mBatteryLight.setColor(mBatteryLowARGB);mLight = new com.android.internal.policy.Light(Light.CHARGING, 3000, 5000, mBatteryLowARGB);} else {// Flash red when battery is low and not charging// mBatteryLight.setFlashing(mBatteryLowARGB,// LightsService.LIGHT_FLASH_TIMED,// batteryLowLedOn, batteryLowLedOff);mLight = new com.android.internal.policy.Light(Light.LOW_BATTERY, batteryLowLedOn,batteryLowLedOff, mBatteryLowARGB);}try {mLightsService.open(mLight);} catch (Exception e) {e.printStackTrace();}} else {// mBatteryLight.turnOff();try {if (mLight != null)mLightsService.close(mLight.getType());elsemLightsService.close(com.android.internal.policy.Light.POWER_ALL); //i dont have time to test,set this to avoid the light not off} catch (Exception e) {e.printStackTrace();}if (level < 90) {mLight = new com.android.internal.policy.Light(Light.CHARGING, 3000, 5000, mBatteryMediumARGB);}else if(level >= 90){mLight = new com.android.internal.policy.Light(Light.CHARGE_COMPLETE,3000, 5000,mBatteryFullARGB);}else if (status == BatteryManager.BATTERY_STATUS_FULL){// mBatteryLight.setColor(mBatteryFullARGB);mLight = new com.android.internal.policy.Light(Light.CHARGE_COMPLETE, 0, 0, mBatteryFullARGB);}try {mLightsService.open(mLight);} catch (Exception e) {e.printStackTrace();}}
總結
以上是生活随笔為你收集整理的获取手机电池的剩余电量的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: range
- 下一篇: 如何在苹果商店发布App?