Android提供的系統(tǒng)服務(wù)之--PowerManager(電源服務(wù))
來源:程序員人生 發(fā)布時(shí)間:2015-04-07 08:33:48 閱讀次數(shù):3043次
Android提供的系統(tǒng)服務(wù)之--PowerManager(電源服務(wù))
――轉(zhuǎn)載請注明出處:coder-pig
本節(jié)引言:
本節(jié)主要講授的Android為我們提供的系統(tǒng)服務(wù)中的:PowerManager電源管理的1個(gè)API,
用于管理CPU運(yùn)行,鍵盤或屏幕亮起來;不過,除非是迫不得已吧,不然的話,否則應(yīng)當(dāng)盡可能避免
使用這個(gè)類,并且使用完以后1定要及時(shí)釋放!本節(jié)其實(shí)不太深入的去講授,由于這個(gè)設(shè)計(jì)到底層的
1些東西,以后需要用到再深入研究,到時(shí)再另外寫1篇blog總結(jié)!所以本節(jié)介紹的主要是
1些基本的概念,PowerManager,wakelock鎖的機(jī)制等等!
本節(jié)知識點(diǎn)圖:

代碼示例:
1個(gè)管理WakeLock和Android裝備電源管理的工具類:
import android.content.Context;
import android.content.SharedPreferences;
import android.os.PowerManager;
import android.preference.PreferenceManager;
public class ManageWakeLock {
private static PowerManager.WakeLock mWakeLock = null;
private static PowerManager.WakeLock mPartialWakeLock = null;
private static final boolean PREFS_SCREENON_DEFAULT = true;
private static final boolean PREFS_DIMSCREEN_DEFAULT = false;
private static final String PREFS_TIMEOUT_DEFAULT = "30";
public static synchronized void acquireFull(Context mContext) {
if (mWakeLock != null) {
return;
}
PowerManager mPm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
int flags;
flags = PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP;
mWakeLock = mPm.newWakeLock(flags, “ManagerWakeLock”);
mWakeLock.setReferenceCounted(false);
mWakeLock.acquire();
}
public static synchronized void acquirePartical(Context mContext) {
//Check if partial lock already exists
if (mPartialWakeLock != null) {
return;
}
PowerManager mPm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
mPartialWakeLock = mPm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP, “ManagerWakeLock”);
mPartialWakeLock.setReferenceCounted(false);
mPartialWakeLock.acquire();
}
}
public static synchronized void releaseFull() {
if (mWakeLock != null) {
mWakeLock.release();
mWakeLock = null;
}
}
public static synchronized void releasePartial() {
if (mPartialWakeLock != null) {
mPartialWakeLock.release();
mPartialWakeLock = null;
}
}
public static synchronized void releaseAll() {
releaseFull();
releasePartial();
}
}
參考資料:
http://www.open-open.com/lib/view/open1343207436974.html
http://blog.csdn.net/xieqibao/article/details/6562256
http://blog.chinaunix.net/uid⑵7411029-id⑷040727.html
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈