Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android電池狀態監聽

Android電池狀態監聽

編輯:關於Android編程

如果要監聽電池的狀態改變,需要動態注冊:android.intent.action.BATTERY_CHANGED,收到Action後可以根據對應的Key獲取你需要的信息,更詳細信息可以參考以下例子中的BatteryChangedReceiver類

package com.example.charginganimation;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {
	private CustomClipLoading ccl;
	private BatteryChangedReceiver receiver = new BatteryChangedReceiver();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		registerReceiver(receiver, getFilter());
	}

	private IntentFilter getFilter() {
		IntentFilter filter = new IntentFilter();
		filter.addAction(Intent.ACTION_BATTERY_CHANGED);
		filter.addAction(Intent.ACTION_BATTERY_LOW);
		filter.addAction(Intent.ACTION_BATTERY_OKAY);
		return filter;
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		unregisterReceiver(receiver);
		super.onDestroy();
	}

	class BatteryChangedReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			final String action = intent.getAction();
			if (action.equalsIgnoreCase(Intent.ACTION_BATTERY_CHANGED)) {
				System.out
						.println("BatteryChangedReceiver BATTERY_CHANGED_ACTION---");
				// 當前電池的電壓
				int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE,
						-1);
				// 電池的健康狀態
				int health = intent
						.getIntExtra(BatteryManager.EXTRA_HEALTH, -1);
				switch (health) {
				case BatteryManager.BATTERY_HEALTH_COLD:
					System.out.println("BATTERY_HEALTH_COLD");
					break;
				case BatteryManager.BATTERY_HEALTH_DEAD:
					System.out.println("BATTERY_HEALTH_DEAD ");
					break;
				case BatteryManager.BATTERY_HEALTH_GOOD:
					System.out.println("BATTERY_HEALTH_GOOD");
					break;
				case BatteryManager.BATTERY_HEALTH_OVERHEAT:
					System.out.println("BATTERY_HEALTH_OVERHEAT");
					break;
				case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
					System.out.println("BATTERY_HEALTH_COLD");
					break;
				case BatteryManager.BATTERY_HEALTH_UNKNOWN:
					System.out.println("BATTERY_HEALTH_UNKNOWN");
					break;
				case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
					System.out.println("BATTERY_HEALTH_UNSPECIFIED_FAILURE");
					break;
				default:
					break;
				}
				// 電池當前的電量, 它介於0和 EXTRA_SCALE之間
				int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
				// 電池電量的最大值
				int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
				// 當前手機使用的是哪裡的電源
				int pluged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,
						-1);
				switch (pluged) {
				case BatteryManager.BATTERY_PLUGGED_AC:
					// 電源是AC charger.[應該是指充電器]
					System.out.println("BATTERY_PLUGGED_AC");
					break;
				case BatteryManager.BATTERY_PLUGGED_USB:
					// 電源是USB port
					System.out.println("BATTERY_PLUGGED_USB ");
					break;
				default:
					break;
				}
				int status = intent
						.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
				switch (status) {
				case BatteryManager.BATTERY_STATUS_CHARGING:
					// 正在充電
					System.out.println("BATTERY_STATUS_CHARGING ");
					break;
				case BatteryManager.BATTERY_STATUS_DISCHARGING:
					System.out.println("BATTERY_STATUS_DISCHARGING  ");
					break;
				case BatteryManager.BATTERY_STATUS_FULL:
					// 充滿
					System.out.println("BATTERY_STATUS_FULL ");
					break;
				case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
					// 沒有充電
					System.out.println("BATTERY_STATUS_NOT_CHARGING ");
					break;
				case BatteryManager.BATTERY_STATUS_UNKNOWN:
					// 未知狀態
					System.out.println("BATTERY_STATUS_UNKNOWN ");
					break;
				default:
					break;
				}
				// 電池使用的技術。比如,對於锂電池是Li-ion
				String technology = intent
						.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY);
				// 當前電池的溫度
				int temperature = intent.getIntExtra(
						BatteryManager.EXTRA_TEMPERATURE, -1);
				System.out.println("voltage = " + voltage + " technology = "
						+ technology + " temperature = " + temperature
						+ " level = " + level + " scale = " + scale);
			} else if (action.equalsIgnoreCase(Intent.ACTION_BATTERY_LOW)) {
				// 表示當前電池電量低
				System.out
						.println("BatteryChangedReceiver ACTION_BATTERY_LOW---");
			} else if (action.equalsIgnoreCase(Intent.ACTION_BATTERY_OKAY)) {
				// 表示當前電池已經從電量低恢復為正常
				System.out
						.println("BatteryChangedReceiver ACTION_BATTERY_OKAY---");
			}
		}

	}
}

獲取apk和版本信息

private int getVerCode(Context context) {
		int verCode = -1;
		try {
			verCode = context.getPackageManager().getPackageInfo(
					"com.example.charginganimation", 0).versionCode;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return verCode;
	}

private String getVerName(Context context) {
		String verName = "";
		try {
			verName = context.getPackageManager().getPackageInfo(
					"com.example.charginganimation", 0).versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return verName;
	}



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved