Java Code Examples for android.os.PowerManager.WakeLock#acquire()

The following examples show how to use android.os.PowerManager.WakeLock#acquire() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WakeLockUtil.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @param tag will be prefixed with "signal:" if it does not already start with it.
 */
public static WakeLock acquire(@NonNull Context context, int lockType, long timeout, @NonNull String tag) {
  tag = prefixTag(tag);
  try {
    PowerManager powerManager = ServiceUtil.getPowerManager(context);
    WakeLock     wakeLock     = powerManager.newWakeLock(lockType, tag);

    wakeLock.acquire(timeout);
    Log.d(TAG, "Acquired wakelock with tag: " + tag);

    return wakeLock;
  } catch (Exception e) {
    Log.w(TAG, "Failed to acquire wakelock with tag: " + tag, e);
    return null;
  }
}
 
Example 2
Source File: WakefulBroadcastReceiver.java    From letv with Apache License 2.0 6 votes vote down vote up
public static ComponentName startWakefulService(Context context, Intent intent) {
    ComponentName comp;
    synchronized (mActiveWakeLocks) {
        int id = mNextId;
        mNextId++;
        if (mNextId <= 0) {
            mNextId = 1;
        }
        intent.putExtra(EXTRA_WAKE_LOCK_ID, id);
        comp = context.startService(intent);
        if (comp == null) {
            comp = null;
        } else {
            WakeLock wl = ((PowerManager) context.getSystemService("power")).newWakeLock(1, "wake:" + comp.flattenToShortString());
            wl.setReferenceCounted(false);
            wl.acquire(60000);
            mActiveWakeLocks.put(id, wl);
        }
    }
    return comp;
}
 
Example 3
Source File: ScreenLockUtil.java    From AndroidModulePattern with Apache License 2.0 6 votes vote down vote up
/**
 * 保持屏幕常亮
 *
 * @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, "开启屏幕常亮");
}
 
Example 4
Source File: MqttService.java    From Sparkplug with Eclipse Public License 1.0 6 votes vote down vote up
@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();
}
 
Example 5
Source File: ApplicationMigrationService.java    From Silence with GNU General Public License v3.0 6 votes vote down vote up
@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();
  }
}
 
Example 6
Source File: WakeLockManager.java    From OmniList with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Acquires a partial {@link WakeLock}, stores it internally and puts the
 * tag into the {@link Intent}. To be used with {@link WakeLockManager#releasePartialWakeLock(Intent)}
 *
 * @param intent intent
 * @param wlTag tag */
public void acquirePartialWakeLock(Intent intent, String wlTag) {
    final WakeLock wl = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, wlTag);
    wl.acquire();
    wakeLocks.add(wl);
    intent.putExtra(WakeLockManager.EXTRA_WAKELOCK_HASH, wl.hashCode());
    intent.putExtra(WakeLockManager.EXTRA_WAKELOCK_TAG, wlTag);
    LogUtils.d("acquirePartialWakeLock: " + wl.toString() + " " + wlTag + " was acquired");
}
 
Example 7
Source File: DbBindingService.java    From Onosendai with Apache License 2.0 5 votes vote down vote up
@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();
	}
}
 
Example 8
Source File: Lock.java    From Kernel-Tuner with GNU General Public License v3.0 5 votes vote down vote up
public static synchronized void acquire(Context context) {
	WakeLock wakeLock = getLock(context);
	if (!wakeLock.isHeld()) {
		wakeLock.acquire();
		//Log.d("alogcat", "wake lock acquired");
	}
}
 
Example 9
Source File: MQTTService.java    From appcan-android with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void acquireWakelock(WakeLock wl, String tag) {
    try {
        wl.acquire();
    } catch (Exception e) {
        PushReportUtility.oe(tag, e);
    }
}
 
Example 10
Source File: MediaButtonReceiver.java    From media-button-router with Apache License 2.0 5 votes vote down vote up
/**
 * Shows the selector dialog that allows the user to decide which music
 * player should receiver the media button press intent.
 * 
 * @param context
 *            The context.
 * @param intent
 *            The intent to forward.
 * @param keyEvent
 *            The key event
 */
private void showSelector(Context context, Intent intent, KeyEvent keyEvent) {
    KeyguardManager manager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
    boolean locked = manager.inKeyguardRestrictedInputMode();

    Intent showForwardView = new Intent(Constants.INTENT_ACTION_VIEW_MEDIA_BUTTON_LIST);
    showForwardView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    showForwardView.putExtras(intent);
    showForwardView.setClassName(context,
            locked ? ReceiverSelectorLocked.class.getName() : ReceiverSelector.class.getName());

    /* COMMENTED OUT FOR MARKET RELEASE Log.i(TAG, "Media Button Receiver: starting selector activity for keyevent: " + keyEvent); */

    if (locked) {

        // XXX See if this actually makes a difference, might
        // not be needed if we move more things to onCreate?
        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        // acquire temp wake lock
        WakeLock wakeLock = powerManager.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP
                | PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, TAG);
        wakeLock.setReferenceCounted(false);

        // Our app better display within 3 seconds or we have
        // bigger issues.
        wakeLock.acquire(3000);
    }
    context.startActivity(showForwardView);
}
 
Example 11
Source File: MyService.java    From Dendroid-HTTP-RAT with GNU General Public License v3.0 5 votes vote down vote up
@SuppressLint("Wakelock") @Override
  protected String doInBackground(String... params) {     
  	PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
  	final WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE, "");
  	wl.acquire();
return "Executed";
  }
 
Example 12
Source File: MyService.java    From BetterAndroRAT with GNU General Public License v3.0 5 votes vote down vote up
@Override
  protected String doInBackground(String... params) {     
  	PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
  	final WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE, "");
  	wl.acquire();
return "Executed";
  }
 
Example 13
Source File: InCallWakeLockUtils.java    From react-native-incall-manager with ISC License 5 votes vote down vote up
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;
}
 
Example 14
Source File: InCallWakeLockUtils.java    From flutter-incall-manager with ISC License 5 votes vote down vote up
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;
}
 
Example 15
Source File: SettingsDownloader.java    From SettingsDeployer with MIT License 4 votes vote down vote up
@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;
}
 
Example 16
Source File: AlarmReceiver.java    From Onosendai with Apache License 2.0 4 votes vote down vote up
private static void aquireTempWakeLock (final Context context) {
	final PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
	final WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, C.TAG);
	wl.acquire(TEMP_WAKELOCK_TIMEOUT_MILLIS);
}
 
Example 17
Source File: MyScreenLocker.java    From SimplePomodoro-android with MIT License 4 votes vote down vote up
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();
}