Example usage for android.os PowerManager PARTIAL_WAKE_LOCK

List of usage examples for android.os PowerManager PARTIAL_WAKE_LOCK

Introduction

In this page you can find the example usage for android.os PowerManager PARTIAL_WAKE_LOCK.

Prototype

int PARTIAL_WAKE_LOCK

To view the source code for android.os PowerManager PARTIAL_WAKE_LOCK.

Click Source Link

Document

Wake lock level: Ensures that the CPU is running; the screen and keyboard backlight will be allowed to go off.

Usage

From source file:com.miljin.setminder.TimerService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    //keep CPU on while timer is ticking
    PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    final PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
            "MyWakelockTag");
    wakeLock.acquire();//w ww .j  a  v  a 2 s . c  o m

    seconds = intent.getLongExtra(MainActivity.EXTRA_START_TIME, 0);

    Intent getActivityIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, getActivityIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    //make notification
    notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder
            .setContentTitle(getResources().getString(com.miljin.setminder.R.string.notification_title));
    if (seconds >= 120) {
        notificationBuilder.setContentText(Long.toString(seconds / 60)
                + getResources().getString(com.miljin.setminder.R.string.notification_text_plural));
    } else {
        notificationBuilder.setContentText(
                getResources().getString(com.miljin.setminder.R.string.notification_text_minute));
    }
    notificationBuilder.setSmallIcon(com.miljin.setminder.R.drawable.ic_notification);
    notificationBuilder.setContentIntent(pendingIntent);
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(notifyID, notificationBuilder.build());

    myCountDownTimer = new CountDownTimer(seconds * 1000, 100) {
        public void onTick(long millisUntilFinished) {
            timerServiceTicking = true;
            broadcastTime(millisUntilFinished);
            sendNotification(millisUntilFinished);
        }

        public void onFinish() {
            timerServiceTicking = false;
            broadcastDone();
            wakeLock.release();
        }
    };
    myCountDownTimer.start();

    return START_NOT_STICKY;
}

From source file:ca.lightseed.winston.WinstonService.java

/**
 * Creates the partial wake-lock which keeps CPU running, and allows this service
 * to continue passing location data even with screen off.
 * NOTE: WakeLocks consume battery very quickly.
 *///from www  . j a  v  a  2s  . c  om
@Override
public void onCreate() {
    super.onCreate();
    PowerManager pm = (PowerManager) getSystemService(this.POWER_SERVICE);

    wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DoNotSleep");
    // TODO: Intrusively many toasts are generated. Fine for testing, not fine for release.
    Toast.makeText(this, "Created Winston tracking service", Toast.LENGTH_SHORT).show();
}

From source file:com.android.mms.quickmessage.QuickMessageWear.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = this;
    Intent i = getIntent();/*  ww  w.j a  v  a 2  s.com*/
    parseIntent(i);
    //Get partial Wakelock so that we can send the message even if phone is locked
    final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
    mWakeLock.acquire();
}

From source file:com.ilearnrw.reader.tasks.AddToLibraryTask.java

public void run(String... params) {

    wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL, "wifiLock");
    wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "wakeLock");

    wifiLock.acquire();//w w  w . j  av  a  2s. c  om
    wakeLock.acquire();
    this.execute(params);
}

From source file:eu.faircode.netguard.DownloadTask.java

@Override
protected void onPreExecute() {
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
    wakeLock.acquire();/*w  w  w .  java2 s  .co  m*/
    showNotification(0);
    Toast.makeText(context, context.getString(R.string.msg_downloading, url.toString()), Toast.LENGTH_SHORT)
            .show();
}

From source file:us.dustinj.locationstore.io.LocationExporter.java

public LocationExporter(Context context, HttpStatusResponseHandler responseReceiver) {
    m_context = context;//from www  .j a  va  2 s. c o  m
    m_responseReceiver = responseReceiver;
    m_appPrefs = AppSettings.GetSettings(context);

    PowerManager pm = (PowerManager) m_context.getSystemService(Context.POWER_SERVICE);
    m_wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, this.getClass().getName());
}

From source file:com.rks.musicx.services.MediaButtonReceiver.java

private static void acquireWakeLockAndSendMessage(Context context, Message msg, long delay) {
    if (mWakeLock == null) {
        Context appContext = context.getApplicationContext();
        PowerManager pm = (PowerManager) appContext.getSystemService(Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Musicx headset button");
        mWakeLock.setReferenceCounted(false);
    }//from  w  ww. j  av  a 2  s .c  o m
    // Make sure we don't indefinitely hold the wake lock under any circumstances
    mWakeLock.acquire(10000);
    mHandler.sendMessageDelayed(msg, delay);
}

From source file:net.kayateia.lifestream.UploadService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i(LOG_TAG, "UploadService kicked");

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    final PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");

    // See CaptureService for comments on this wake lock arrangement.
    boolean needsRelease = true;
    try {/*from  w w w  .  jav a  2s. co m*/
        // If we don't release it within the timeout somehow, release it anyway.
        // This may interrupt a long backlog upload or something, but this
        // is really about as long as we can reasonably expect to take.
        Log.v(LOG_TAG, "Acquire wakelock");
        if (wl != null)
            wl.acquire(WAKELOCK_TIMEOUT);
        if (!Network.IsActive(this)) {
            Log.i(LOG_TAG, "No network active, giving up");
            return START_NOT_STICKY;
        }

        // Do the check in a thread.
        new AsyncTask<UploadService, Void, Void>() {
            @Override
            protected Void doInBackground(UploadService... svc) {
                svc[0].checkUploads();
                return null;
            }

            @Override
            protected void onPostExecute(Void foo) {
                Log.v(LOG_TAG, "Release wakelock by worker");
                if (wl != null && wl.isHeld())
                    wl.release();
            }
        }.execute(this);
        needsRelease = false;
    } finally {
        if (needsRelease) {
            Log.v(LOG_TAG, "Release wakelock by default");
            if (wl != null && wl.isHeld())
                wl.release();
        }
    }

    // There's no need to worry about this.. we'll get re-kicked by the alarm.
    return START_NOT_STICKY;
}

From source file:org.ntpsync.service.NtpSyncService.java

private static void getLocks(Context context) {
    // initialise the lock
    wakeLock = ((PowerManager) context.getSystemService(Context.POWER_SERVICE))
            .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "NtpSyncWakeLock");
}

From source file:net.kayateia.lifestream.StreamService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.v(LOG_TAG, "Kicked");

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    final PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");

    // See CaptureService for comments on this wake lock arrangement.
    boolean needsRelease = true;
    try {/*from  w ww .  j av  a  2s  . com*/
        // If we don't release it within the timeout somehow, release it anyway.
        // This may interrupt a long backlog download or something, but this
        // is really about as long as we can reasonably expect to take.
        Log.v(LOG_TAG, "Acquire wakelock");
        if (wl != null)
            wl.acquire(WAKELOCK_TIMEOUT);

        if (!Network.IsActive(this)) {
            Log.i(LOG_TAG, "No network active, giving up");
            return START_NOT_STICKY;
        }

        // Do the check in a thread.
        new AsyncTask<StreamService, Void, Void>() {
            @Override
            protected Void doInBackground(StreamService... svc) {
                svc[0].checkNewImages();
                return null;
            }

            @Override
            protected void onPostExecute(Void foo) {
                Log.v(LOG_TAG, "Release wakelock by worker");
                if (wl != null && wl.isHeld())
                    wl.release();
            }
        }.execute(this);
        needsRelease = false;
    } finally {
        if (needsRelease) {
            Log.v(LOG_TAG, "Release wakelock by default");
            if (wl != null && wl.isHeld())
                wl.release();
        }
    }

    // There's no need to worry about this.. we'll get re-kicked by the alarm.
    return START_NOT_STICKY;
}