編輯:Android開發實例
今天想使用android系統的電池偵聽和系統的電池圖片;
引入源代碼中的兩個xml文件(這裡我在android2.2的jar包中找不到,所以才引入,源代碼路徑:C:\android-sdk-windows\platforms\android-8\data\res\drawable),之後會發現stat_sys_battery.xml報錯,所以我就將下圖
這些圖引入到我的drawable目錄下,然後將stat_sys_battery.xml中的圖片引入路徑更改,如下:
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:maxLevel="4" android:drawable="@drawable/stat_sys_battery_0" />
<item android:maxLevel="14" android:drawable="@drawable/stat_sys_battery_10" />
<item android:maxLevel="29" android:drawable="@drawable/stat_sys_battery_20" />
<item android:maxLevel="49" android:drawable="@drawable/stat_sys_battery_40" />
<item android:maxLevel="69" android:drawable="@drawable/stat_sys_battery_60" />
<item android:maxLevel="89" android:drawable="@drawable/stat_sys_battery_80" />
<item android:maxLevel="100" android:drawable="@drawable/stat_sys_battery_100" />
</level-list>
接下來是在activity中的操作:
創建一個IntentFilter
private IntentFilter mIntentFilter;
在oncreate的方法中:加入:
//電池相關
mIntentFilter = new IntentFilter();
mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
//聲明消息處理過程
private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
int status = intent.getIntExtra("status", 0);
int health = intent.getIntExtra("health", 0);
boolean present = intent.getBooleanExtra("present", false);
int level = intent.getIntExtra("level", 0);
int scale = intent.getIntExtra("scale", 0);
int icon_small = intent.getIntExtra("icon-small", 0);
int plugged = intent.getIntExtra("plugged", 0);
int voltage = intent.getIntExtra("voltage", 0);
int temperature = intent.getIntExtra("temperature", 0);
String technology = intent.getStringExtra("technology");
String statusString = "";
switch (status) {
case BatteryManager.BATTERY_STATUS_UNKNOWN:
statusString = "unknown";
break;
case BatteryManager.BATTERY_STATUS_CHARGING:
statusString = "charging";
battery_image.setImageResource(R.drawable.stat_sys_battery_charge);
battery_image.getDrawable().setLevel(level);
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
statusString = "discharging";
battery_image.setImageResource(R.drawable.stat_sys_battery);
battery_image.getDrawable().setLevel(level);
break;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
statusString = "not charging";
break;
case BatteryManager.BATTERY_STATUS_FULL:
statusString = "full";
break;
}
String healthString = "";
switch (health) {
case BatteryManager.BATTERY_HEALTH_UNKNOWN:
healthString = "unknown";
break;
case BatteryManager.BATTERY_HEALTH_GOOD:
healthString = "good";
break;
case BatteryManager.BATTERY_HEALTH_OVERHEAT:
healthString = "overheat";
break;
case BatteryManager.BATTERY_HEALTH_DEAD:
healthString = "dead";
break;
case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
healthString = "voltage";
break;
case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
healthString = "unspecified failure";
break;
}
String acString = "";
switch (plugged) {
case BatteryManager.BATTERY_PLUGGED_AC:
acString = "plugged ac";
break;
case BatteryManager.BATTERY_PLUGGED_USB:
acString = "plugged usb";
break;
}
Log.i("cat", statusString);
Log.i("cat", healthString);
Log.i("cat", String.valueOf(present));
Log.i("cat", String.valueOf(level));
Log.i("cat", String.valueOf(scale));
Log.i("cat", String.valueOf(icon_small));
Log.i("cat", acString);
Log.i("cat", String.valueOf(voltage));
Log.i("cat", String.valueOf(temperature));
Log.i("cat", technology);
//要看看是不是我們要處理的消息
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", "ss" + intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_CHARGING));
//充電類型 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));
}
}
};
——————————————————————————————-
這裡真正處理圖片變換的是以下幾句話:其他的我是在做備注
switch (status) {
case BatteryManager.BATTERY_STATUS_UNKNOWN:
statusString = "unknown";
break;
case BatteryManager.BATTERY_STATUS_CHARGING:
statusString = "charging";
battery_image.setImageResource(R.drawable.stat_sys_battery_charge);
battery_image.getDrawable().setLevel(level);
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
statusString = "discharging";
battery_image.setImageResource(R.drawable.stat_sys_battery);
battery_image.getDrawable().setLevel(level);
break;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
statusString = "not charging";
break;
case BatteryManager.BATTERY_STATUS_FULL:
statusString = "full";
break;
}
在onResume方法中注冊消息處理器
registerReceiver(mIntentReceiver, mIntentFilter);
在onPause方法中注銷
unregisterReceiver(mIntentReceiver);
分類 notification有以下幾種: 1>普通notification 1.內容標題 2.大圖標 3.內
這篇文章主要為大家詳細介紹了Android實現頂部導航菜單左右滑動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下 本文給大家介紹在Android中如何實現頂部導
文件main.java代碼如下:package com.HHBrowser.android;import android.app.Activity;import
JSON代表JavaScript對象符號。它是一個獨立的數據交換格式,是XML的最佳替代品。本章介紹了如何解析JSON文件,並從中提取所需的信息。Android提供了四個