下面列出了android.os.PowerManager.WakeLock#release ( ) 实例代码,或者点击链接到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");
}
}
public static boolean completeWakefulIntent(Intent intent) {
int id = intent.getIntExtra(EXTRA_WAKE_LOCK_ID, 0);
if (id == 0) {
return false;
}
synchronized (mActiveWakeLocks) {
WakeLock wl = (WakeLock) mActiveWakeLocks.get(id);
if (wl != null) {
wl.release();
mActiveWakeLocks.remove(id);
return true;
}
Log.w("WakefulBroadcastReceiver", "No active wake lock id #" + id);
return true;
}
}
@Override
@SuppressLint("Wakelock")
public void onReceive(Context context, Intent intent) {
traceDebug(TAG, "Internal network status receive.");
// we protect against the phone switching off
// by requesting a wake lock - we request the minimum possible wake
// lock - just enough to keep the CPU running until we've finished
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
WakeLock wl = pm
.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MQTT");
wl.acquire();
traceDebug(TAG,"Reconnect for Network recovery.");
if (isOnline()) {
traceDebug(TAG,"Online,reconnect.");
// we have an internet connection - have another try at
// connecting
reconnect();
} else {
notifyClientsOffline();
}
wl.release();
}
@Override
public void run() {
notification = initializeBackgroundNotification();
PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Migration");
try {
wakeLock.acquire();
setState(new ImportState(ImportState.STATE_MIGRATING_BEGIN, null));
SmsMigrator.migrateDatabase(ApplicationMigrationService.this,
masterSecret,
ApplicationMigrationService.this);
setState(new ImportState(ImportState.STATE_MIGRATING_COMPLETE, null));
setDatabaseImported(ApplicationMigrationService.this);
stopForeground(true);
notifyImportComplete();
stopSelf();
} finally {
wakeLock.release();
}
}
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 _releaseWakeLock(WakeLock lock) {
synchronized (lock) {
if (lock.isHeld()) {
lock.release();
return true;
}
}
return false;
}
private void releaseWakelock(WakeLock wl, String tag) {
try {
wl.release();
} catch (Exception e) {
PushReportUtility.oe(tag, e);
}
}
@Override
protected final void onHandleIntent (final Intent i) {
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
final WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, C.TAG);
wl.acquire();
try {
doWork(i);
}
finally {
wl.release();
}
}
@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;
}
public void unlock(){
PowerManager pm = (PowerManager)mActivity.getSystemService(Context.POWER_SERVICE);
WakeLock mWakelock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.SCREEN_DIM_WAKE_LOCK, "SimpleTimer");
mWakelock.acquire();
mWakelock.release();
}