編輯:關於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; }
之前學習過了MediaPlayer用於播放手機音樂,但是在手機中很多的提示音並不是使用MediaPlayer來播放的比如短信鈴聲,通知鈴聲,android中使用Sound
雖然之前也分析過View回執過程,但是如果讓我自己集成ViewGroup然後自己重新onMeasure,onLayout,onDraw方法自定義View我還是會頭疼。今天
效果項目地址DraggableTableView所有Cell都可以拖拽。固定第一個Cell限制長按區域實現原理對UITableView添加LongPress手勢 在lon
效果圖如圖所示:注解:此方法類 是本人在網上找的。並非本人的,拿來自己修改就直接用了import me.imid.movablecheckbox.R;import and