極力推薦Android 開發大總結文章:歡迎收藏
Android 開發技術文章大總結
本篇文章主要介紹 Android
開發中的部分知識點,通過閱讀本篇文章,您將收獲以下內容:
一、 Framework 層字符串添加
二、Service 中實時監測 電池異常溫度並彈窗提醒用戶
檢測電池溫度,提示用戶溫度異常,請注意
Android
電池信息狀態主要是在frameworks/base/services/core/java/com/android/server/BatteryService.java
,本文也是基於此BatteryService
實時監測 電池溫度,及時提醒用戶,為了安全起見,在電池溫度異常時候,請勿繼續充電。
一、 Framework 層字符串添加
1.添加彈窗字符串資源
alps/frameworks/base/core/res/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
... ...
<!-- add NTC Test Case-->
<string name="warningLowBatteryTemperature">"Battery temperature is too low, in order to protect battery life, charging function will be temporarily stopped."</string>
<string name="warningHightBatteryTemperature">"Battery temperature is too high, in order to protect battery life, charging function will be temporarily stopped."</string>
<string name="shutdownBatteryTemperatureMsg">"Battery temperature is too high. For safety reasons, the phone will shut down automatically later."</string>
<string name="batteryTemperatureWarning">Warning</string>
<string name="batteryTemperatureOk">"OK"</string>
<!-- add NTC Test Case-->
... ...
</resources>
2.添加自定義電池異常溫度定義
自定義溫度 主要修改alps/frameworks/base/core/res/res/values/config.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
... ...
<!-- Shutdown if the battery temperature exceeds (this value * 0.1) Celsius. -->
<integer name="config_shutdownBatteryTemperature">680</integer>
<!-- add NTC Test Case-->
<integer name="config_warningHightBatteryTemperature">550</integer>
<integer name="config_warningLowBatteryTemperature">0</integer>
<!-- add NTC Test Case-->
... ...
</resources>
3. 在symbols 文件中添加對應java-symbol方便Framework代碼引用
在symbols
文件中為所有string
,int
值注冊,方便Framework
層代碼的引用。
alps/frameworks/base/core/res/res/values/symbols.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Private symbols that we need to reference from framework code. See
frameworks/base/core/res/MakeJavaSymbols.sed for how to easily generate
this.
Can be referenced in java code as: com.android.internal.R.<type>.<name>
and in layout xml as: "@*android:<type>/<name>"
-->
... ...
<java-symbol type="integer" name="config_shutdownBatteryTemperature" />
<!-- add NTC Test Case-->
<java-symbol type="integer" name="config_warningHightBatteryTemperature" />
<java-symbol type="integer" name="config_warningLowBatteryTemperature" />
<java-symbol type="string" name="warningLowBatteryTemperature" />
<java-symbol type="string" name="warningHightBatteryTemperature" />
<java-symbol type="string" name="shutdownBatteryTemperatureMsg" />
<java-symbol type="string" name="batteryTemperatureWarning" />
<java-symbol type="string" name="batteryTemperatureOk" />
<!-- add NTC Test Case-->
二、Service 中實時監測 電池異常溫度並彈窗提醒用戶
BatteryService
后台服務可以實時監測 電池問題,當電池溫度異常時候,我們要及時提醒用戶,如果此時用戶正在充電,提示請勿充電等。
實現方法如下:
//add NTC Test Case
import android.app.AlertDialog;
import android.view.WindowManager;
import android.content.DialogInterface;
//add NTC Test Case
... ...
/**
* <p>BatteryService monitors the charging status, and charge level of the device
* battery. When these values change this service broadcasts the new values
* to all {@link android.content.BroadcastReceiver IntentReceivers} that are
* watching the {@link android.content.Intent#ACTION_BATTERY_CHANGED
* BATTERY_CHANGED} action.</p>
* <p>The new values are stored in the Intent data and can be retrieved by
* calling {@link android.content.Intent#getExtra Intent.getExtra} with the
* following keys:</p>
* <p>"scale" - int, the maximum value for the charge level</p>
* <p>"level" - int, charge level, from 0 through "scale" inclusive</p>
* <p>"status" - String, the current charging status.<br />
* <p>"health" - String, the current battery health.<br />
* <p>"present" - boolean, true if the battery is present<br />
* <p>"icon-small" - int, suggested small icon to use for this state</p>
* <p>"plugged" - int, 0 if the device is not plugged in; 1 if plugged
* into an AC power adapter; 2 if plugged in via USB.</p>
* <p>"voltage" - int, current battery voltage in millivolts</p>
* <p>"temperature" - int, current battery temperature in tenths of
* a degree Centigrade</p>
* <p>"technology" - String, the type of battery installed, e.g. "Li-ion"</p>
*
* <p>
* The battery service may be called by the power manager while holding its locks so
* we take care to post all outcalls into the activity manager to a handler.
*
* FIXME: Ideally the power manager would perform all of its calls into the battery
* service asynchronously itself.
* </p>
*/
public final class BatteryService extends SystemService {
// add NTC Test Case 定義電池溫度檢測所需變量
private int mConfigWarningLowBatteryTemperature;
private int mConfigWarningHightBatteryTemperature;
private String mWarningLowBatteryTemperature;
private String mWarningHightBatteryTemperature;
private String mShutdownBatteryTemperatureMsg;
private AlertDialog mbatteryTemperatureDialog = null;
//add NTC Test Case
... ...
public BatteryService(Context context) {
super(context);
... ...
//add NTC Test Case BatteryService 構造獲取電池檢測所需資源
mConfigWarningLowBatteryTemperature= mContext.getResources().getInteger(
com.android.internal.R.integer.config_warningLowBatteryTemperature);
mConfigWarningHightBatteryTemperature= mContext.getResources().getInteger(
com.android.internal.R.integer.config_warningHightBatteryTemperature);
mWarningHightBatteryTemperature = mContext.getResources().getString(
com.android.internal.R.string.warningHightBatteryTemperature);
mShutdownBatteryTemperatureMsg = mContext.getResources().getString(
com.android.internal.R.string.shutdownBatteryTemperatureMsg);
mWarningLowBatteryTemperature = mContext.getResources().getString(
com.android.internal.R.string.warningLowBatteryTemperature);
//add NTC Test Case
}
private void processValuesLocked(boolean force) {
... ...
shutdownIfNoPowerLocked();
shutdownIfOverTempLocked();
//add NTC Test Case
//調用自定義電池溫度異常檢測方法
warningBatteryTemperature();
//add NTC Test Case
... ...
}
... ...
//add NTC Test Case 自定義判斷電池溫度是否異常
private void warningBatteryTemperature(){
if(mPlugType != BATTERY_PLUGGED_NONE && mHealthInfo!=null && mbatteryTemperatureDialog == null){
if (mHealthInfo.batteryTemperature >= mConfigWarningHightBatteryTemperature) {
mHandler.post(new Runnable() {
public void run() {
if(mHealthInfo.batteryTemperature >= (mShutdownBatteryTemperature-10)){
batteryTemperatureDialog(mShutdownBatteryTemperatureMsg);
}else{
batteryTemperatureDialog(mWarningHightBatteryTemperature);
}
}
});
}
if (mHealthInfo.batteryTemperature < mConfigWarningLowBatteryTemperature) {
mHandler.post(new Runnable() {
public void run() {
batteryTemperatureDialog(mWarningLowBatteryTemperature);
}
});
}
}
}
// 自定義電池溫度Dialog彈窗
private void batteryTemperatureDialog(String batteryTemperature) {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle(mContext.getResources().getString(
com.android.internal.R.string.batteryTemperatureWarning));
builder.setCancelable(false);
builder.setMessage(batteryTemperature);
builder.setIconAttribute(android.R.attr.alertDialogIcon);
builder.setPositiveButton(com.android.internal.R.string.batteryTemperatureOk,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
mbatteryTemperatureDialog = null;
}
});
mbatteryTemperatureDialog = builder.create();
mbatteryTemperatureDialog.getWindow().setType(
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
if (mbatteryTemperatureDialog != null&& !mbatteryTemperatureDialog.isShowing()) {
mbatteryTemperatureDialog.show();
}
}
//add NTC Test Case
... ...
}
至此,本篇已結束,如有不對的地方,歡迎您的建議與指正。同時期待您的關注,感謝您的閱讀,謝謝!