編輯:關於Android編程
直接上代碼:
[java]
/* ========================== Battery Start =============================*/
int tBatteryLevel = 0, tBatteryCurLevel = 0;
int tBatteryChargeFlag = -1;
private void Battery(){
int tTempFlag = 0;
ImageButton tTempButton = (ImageButton)findViewById(R.id.top_battery);
tTempButton.setVisibility(View.INVISIBLE);
IntentFilter mIntentFilter = new IntentFilter();
mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
// 聲明消息處理過程
BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if(action.equals(Intent.ACTION_BATTERY_CHANGED)){
//電池電量,數字
int tTempLevel = intent.getIntExtra("level", 0);
//電池最大容量
int tTempScale = intent.getIntExtra("scale", 100);
//電池狀態,返回是一個數字
// BatteryManager.BATTERY_STATUS_CHARGING 表示是充電狀態
// BatteryManager.BATTERY_STATUS_DISCHARGING 放電中
// BatteryManager.BATTERY_STATUS_NOT_CHARGING 未充電
// BatteryManager.BATTERY_STATUS_FULL 電池滿
int tTempStatus = intent.getIntExtra("status", 0);
tTempLevel = (tTempLevel * 100) / tTempScale;
if (tTempLevel > 95)
tBatteryLevel = 5;
else if(tTempLevel > 80)
tBatteryLevel = 4;
else if(tTempLevel > 60)
tBatteryLevel = 3;
else if(tTempLevel > 40)
tBatteryLevel = 2;
else if(tTempLevel > 20)
tBatteryLevel = 1;
else
tBatteryLevel = 0;
if (tTempStatus == BatteryManager.BATTERY_STATUS_CHARGING)
tBatteryChargeFlag = 1;
else
tBatteryChargeFlag = 0;
}
}
};
registerReceiver(mIntentReceiver, mIntentFilter);
}
/* ========================== Battery End =============================*/
/* ========================== Battery Start =============================*/
int tBatteryLevel = 0, tBatteryCurLevel = 0;
int tBatteryChargeFlag = -1;
private void Battery(){
int tTempFlag = 0;
ImageButton tTempButton = (ImageButton)findViewById(R.id.top_battery);
tTempButton.setVisibility(View.INVISIBLE);
IntentFilter mIntentFilter = new IntentFilter();
mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
// 聲明消息處理過程
BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if(action.equals(Intent.ACTION_BATTERY_CHANGED)){
//電池電量,數字
int tTempLevel = intent.getIntExtra("level", 0);
//電池最大容量
int tTempScale = intent.getIntExtra("scale", 100);
//電池狀態,返回是一個數字
// BatteryManager.BATTERY_STATUS_CHARGING 表示是充電狀態
// BatteryManager.BATTERY_STATUS_DISCHARGING 放電中
// BatteryManager.BATTERY_STATUS_NOT_CHARGING 未充電
// BatteryManager.BATTERY_STATUS_FULL 電池滿
int tTempStatus = intent.getIntExtra("status", 0);
tTempLevel = (tTempLevel * 100) / tTempScale;
if (tTempLevel > 95)
tBatteryLevel = 5;
else if(tTempLevel > 80)
tBatteryLevel = 4;
else if(tTempLevel > 60)
tBatteryLevel = 3;
else if(tTempLevel > 40)
tBatteryLevel = 2;
else if(tTempLevel > 20)
tBatteryLevel = 1;
else
tBatteryLevel = 0;
if (tTempStatus == BatteryManager.BATTERY_STATUS_CHARGING)
tBatteryChargeFlag = 1;
else
tBatteryChargeFlag = 0;
}
}
};
registerReceiver(mIntentReceiver, mIntentFilter);
}
/* ========================== Battery End =============================*/
另外還有一份別人的參考代碼:
[java]
檢測到現在在電源狀態:
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)) {
//電池電量,數字
Log.d("Battery", "" + intent.getIntExtra("level", 0));
//電池最大容量
Log.d("Battery", "" + intent.getIntExtra("scale", 0));
//電池伏數
Log.d("Battery", "" + intent.getIntExtra("voltage", 0));
//電池溫度
Log.d("Battery", "" + intent.getIntExtra("temperature", 0));
//電池狀態,返回是一個數字
// BatteryManager.BATTERY_STATUS_CHARGING 表示是充電狀態
// BatteryManager.BATTERY_STATUS_DISCHARGING 放電中
// BatteryManager.BATTERY_STATUS_NOT_CHARGING 未充電
// BatteryManager.BATTERY_STATUS_FULL 電池滿
Log.d("Battery", "" + intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN));
//充電類型 BatteryManager.BATTERY_PLUGGED_AC 表示是充電器,不是這個值,表示是 USB
Log.d("Battery", "" + intent.getIntExtra("plugged", 0));
//電池健康情況,返回也是一個數字
//BatteryManager.BATTERY_HEALTH_GOOD 良好
//BatteryManager.BATTERY_HEALTH_OVERHEAT 過熱
//BatteryManager.BATTERY_HEALTH_DEAD 沒電
//BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE 過電壓
//BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE 未知錯誤
Log.d("Battery", "" + intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN));
}
}
};
檢測到現在在電源狀態:
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)) {
//電池電量,數字
Log.d("Battery", "" + intent.getIntExtra("level", 0));
//電池最大容量
Log.d("Battery", "" + intent.getIntExtra("scale", 0));
//電池伏數
Log.d("Battery", "" + intent.getIntExtra("voltage", 0));
//電池溫度
Log.d("Battery", "" + intent.getIntExtra("temperature", 0));
//電池狀態,返回是一個數字
// BatteryManager.BATTERY_STATUS_CHARGING 表示是充電狀態
// BatteryManager.BATTERY_STATUS_DISCHARGING 放電中
// BatteryManager.BATTERY_STATUS_NOT_CHARGING 未充電
// BatteryManager.BATTERY_STATUS_FULL 電池滿
Log.d("Battery", "" + intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN));
//充電類型 BatteryManager.BATTERY_PLUGGED_AC 表示是充電器,不是這個值,表示是 USB
Log.d("Battery", "" + intent.getIntExtra("plugged", 0));
//電池健康情況,返回也是一個數字
//BatteryManager.BATTERY_HEALTH_GOOD 良好
//BatteryManager.BATTERY_HEALTH_OVERHEAT 過熱
//BatteryManager.BATTERY_HEALTH_DEAD 沒電
//BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE 過電壓
//BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE 未知錯誤
Log.d("Battery", "" + intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN));
}
}
};
最近接了一個項目其中有功能要實現一個清理內存,要求和微信的效果一樣。於是想到用surfaceView而不是繼承view。下面小編給大家解析下實現思路。surfaceVie
android開發中實現單選與多選對話框的代碼非常簡單,具體代碼如下所示:public void myClick(View view) { // 單選對話框 //si
今天開源一個時間滑輪組件,在網上有各種開源比如wheel和其它改寫,多多少少不夠實用和缺陷。 該項目地址位於:https://github.com/apple317/d
一、前言前一篇文章我們介紹了Android中直播視頻技術的基礎大綱知識,這裡就開始一一講解各個知識點,首先主要來看一下視頻直播中的一個重要的基礎核心類:ByteBuffe