下面列出了android.os.PowerManager.WakeLock#isHeld ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @param tag will be prefixed with "signal:" if it does not already start with it.
*/
public static void release(@Nullable WakeLock wakeLock, @NonNull String tag) {
tag = prefixTag(tag);
try {
if (wakeLock == null) {
Log.d(TAG, "Wakelock was null. Skipping. Tag: " + tag);
} else if (wakeLock.isHeld()) {
wakeLock.release();
Log.d(TAG, "Released wakelock with tag: " + tag);
} else {
Log.d(TAG, "Wakelock wasn't held at time of release: " + tag);
}
} catch (Exception e) {
Log.w(TAG, "Failed to release wakelock with tag: " + tag, e);
}
}
/**
* Releases a partial {@link WakeLock} with a tag contained in the given{@link Intent}
*
* @param intent intent*/
private void releasePartialWakeLock(Intent intent) {
if (intent.hasExtra(WakeLockManager.EXTRA_WAKELOCK_TAG)) {
final int hash = intent.getIntExtra(WakeLockManager.EXTRA_WAKELOCK_HASH, -1);
final String tag = intent.getStringExtra(WakeLockManager.EXTRA_WAKELOCK_TAG);
// We use copy on write list. Iterator won't cause ConcurrentModificationException
for (WakeLock wakeLock : wakeLocks) {
if (hash == wakeLock.hashCode()) {
if (wakeLock.isHeld()) {
wakeLock.release();
LogUtils.d("releasePartialWakeLock: " + wakeLock.toString() + " " + tag + " was released");
} else {
LogUtils.d("releasePartialWakeLock: " + wakeLock.toString() + " " + tag + " was already released!");
}
wakeLocks.remove(wakeLock);
return;
}
}
LogUtils.e("releasePartialWakeLock: " + "Hash " + hash + " was not found");
}
}
/**
* 保持屏幕常亮
*
* @param activity you know
*/
public static void keepScreenOn(Activity activity) {
WakeLock wakeLock = mWakeLockArray.get(activity);
if (wakeLock == null) {
PowerManager powerManager = (PowerManager) activity.getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK,
activity.getClass().getName());
}
if (!wakeLock.isHeld()) {
wakeLock.acquire();
}
mWakeLockArray.put(activity, wakeLock);
cancelLockScreen(activity);
Log.i(TAG, "开启屏幕常亮");
}
private boolean _acquireWakeLock(WakeLock lock, long timeout) {
synchronized (lock) {
if (!lock.isHeld()) {
if (timeout > 0) {
lock.acquire(timeout);
} else {
lock.acquire();
}
return true;
}
}
return false;
}
private boolean _releaseWakeLock(WakeLock lock) {
synchronized (lock) {
if (lock.isHeld()) {
lock.release();
return true;
}
}
return false;
}
/**
* 取消屏幕常亮
*
* @param activity you know
*/
public static void cancelKeepScreen(Activity activity) {
WakeLock wakeLock = mWakeLockArray.get(activity);
if (wakeLock != null) {
if (wakeLock.isHeld()) {
wakeLock.release();
}
}
Log.i(TAG, "取消屏幕常亮");
}
private boolean _acquireWakeLock(WakeLock lock, long timeout) {
synchronized (lock) {
if (!lock.isHeld()) {
if (timeout > 0) {
lock.acquire(timeout);
} else {
lock.acquire();
}
return true;
}
}
return false;
}
private boolean _releaseWakeLock(WakeLock lock) {
synchronized (lock) {
if (lock.isHeld()) {
lock.release();
return true;
}
}
return false;
}
public static synchronized void acquire(Context context) {
WakeLock wakeLock = getLock(context);
if (!wakeLock.isHeld()) {
wakeLock.acquire();
//Log.d("alogcat", "wake lock acquired");
}
}
@Override
protected Boolean doInBackground(String... strParams)
{
Boolean result = Boolean.FALSE;
// get a wakelock to hold for the duration of the background work. downloading
// may be slow. extraction usually isn't too slow but also takes a bit of time. limit the wakelock's time!
PowerManager powerManager = (PowerManager)m_ctx.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SettingsDownloaderWakeLock");
wakeLock.acquire(FOUR_MINUTES);
// download the file
String filename = downloadFile(strParams[0]);
if (filename != null)
{
File fSettingsZip = new File(filename);
if (fSettingsZip.exists() && !isCancelled())
{
try
{
m_logWriter.write("Successfully downloaded to: "+filename+"\n");
// extract to wanted directory
String destDir = strParams[1];
boolean bSuccess = extractSettingsZip(fSettingsZip, destDir);
result = (bSuccess ? Boolean.TRUE : Boolean.FALSE);
// delete settings zip
if (fSettingsZip.exists()) {
fSettingsZip.delete();
}
}
catch(Exception e)
{
Log.e("SettingsDownloader", "Error: "+e.toString());
}
}
}
// if our max time hasn't passed but work is done or an error occurred we bail out and release
if (wakeLock.isHeld())
{
wakeLock.release();
}
return result;
}