Example usage for android.os PowerManager newWakeLock

List of usage examples for android.os PowerManager newWakeLock

Introduction

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

Prototype

public WakeLock newWakeLock(int levelAndFlags, String tag) 

Source Link

Document

Creates a new wake lock with the specified level and flags.

Usage

From source file:com.cpd.receivers.LibraryRenewAlarmBroadcastReceiver.java

@Override
public void onReceive(Context context, Intent intent) {

    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, AppTags.UFRGS_MOBILE);
    //Acquire the lock
    wl.acquire();//from  w  w  w.j a v  a 2 s . c o  m

    prefs = context.getSharedPreferences(AppTags.LIBRARY_LOGIN_PREF, Context.MODE_PRIVATE);
    editor = prefs.edit();
    int value = prefs.getInt(AppTags.ALARM_COUNTER, 0);
    value = value + 1;
    editor.putInt(AppTags.ALARM_COUNTER, value);
    editor.commit();

    LibraryLoader loader = new LibraryLoader(context);
    loader.renewBooks(null, true);

    if (value == DAILY_RETRIES) {
        launchNotification(context, context.getString(R.string.we_tried_to_renew_your_items_notification_title),
                context.getString(R.string.verify_return_date_notification_message));
        editor.putInt(AppTags.ALARM_COUNTER, 0);
        editor.commit();
    }

    wl.release();

}

From source file:com.google.android.apps.muzei.sync.TaskQueueService.java

@Override
public int onStartCommand(final Intent intent, int flags, final int startId) {
    if (intent.getAction() == null) {
        stopSelf();/*from  ww w  .j  av  a 2s . c  o  m*/
        return START_NOT_STICKY;
    }

    String action = intent.getAction();
    if (ACTION_DOWNLOAD_CURRENT_ARTWORK.equals(action)) {
        // Handle internal download artwork request
        new DownloadArtworkTask(this) {
            PowerManager.WakeLock lock;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                // This is normally not started by a WakefulBroadcastReceiver so request a
                // new wakelock.
                PowerManager pwm = (PowerManager) getSystemService(POWER_SERVICE);
                lock = pwm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
                lock.acquire(DOWNLOAD_ARTWORK_WAKELOCK_TIMEOUT_MILLIS);
            }

            @Override
            protected void onPostExecute(Boolean success) {
                super.onPostExecute(success);
                if (success) {
                    cancelArtworkDownloadRetries();
                } else {
                    scheduleRetryArtworkDownload();
                }
                if (lock.isHeld()) {
                    lock.release();
                }
                WakefulBroadcastReceiver.completeWakefulIntent(intent);
                stopSelf(startId);
            }
        }.execute();

    }
    return START_REDELIVER_INTENT;
}

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();/*from  ww  w  .  j a va2 s  .com*/
    showNotification(0);
    Toast.makeText(context, context.getString(R.string.msg_downloading, url.toString()), Toast.LENGTH_SHORT)
            .show();
}

From source file:com.android.switchaccess.SwitchAccessService.java

@SuppressWarnings("deprecation")
@Override/*  w w  w.j a v a2 s .  c o m*/
protected void onServiceConnected() {
    sInstance = this;
    mOverlayController = new OverlayController(new SimpleOverlay(this));
    mOverlayController.configureOverlay();
    mOptionManager = new OptionManager(mOverlayController);
    mMainTreeBuilder = new MainTreeBuilder(this);
    mAutoScanController = new AutoScanController(mOptionManager, new Handler(), this);
    mKeyboardEventManager = new KeyboardEventManager(this, mOptionManager, mAutoScanController);
    mAnalytics = new Analytics();
    mAnalytics.start();
    PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE,
            "SwitchAccess");
    SharedPreferencesUtils.getSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
    mActionProcessor = new ActionProcessor(this);
    mEventProcessor = new UiChangeDetector(mActionProcessor);
}

From source file:ufms.br.com.ufmsapp.task.DownloadTask.java

@Override
protected void onPreExecute() {
    super.onPreExecute();
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
    mWakeLock.acquire();/*from   w w  w.  j  a  v  a  2s.co m*/

    builder.setProgress(100, 0, false);
    manager.notify(NOTIFICATION_ID, builder.build());
}

From source file:com.github.sryze.wirebug.DebugStatusService.java

@Override
public void onCreate() {
    super.onCreate();

    Log.d(TAG, "Service is created");

    preferences = PreferenceManager.getDefaultSharedPreferences(this);

    PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, TAG);

    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
    connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
}

From source file:it.baywaylabs.jumpersumo.robot.ServerPolling.java

/**
 * Method auto invoked execute the task.
 *//*from   ww  w .ja  v  a2 s . c  om*/
@Override
protected void onPreExecute() {
    super.onPreExecute();
    // take CPU lock to prevent CPU from going off if the user
    // presses the power button during download
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
    mWakeLock.acquire();
    if (mProgressDialog != null)
        mProgressDialog.show();
}

From source file:osu.crowd_ml.BackgroundDataSend.java

@Override
public void onCreate() {
    super.onCreate();

    // TODO(david soller) : fix these comments
    // Step 1. Extract necessary information
    UID = MultiprocessPreferences.getDefaultSharedPreferences(this).getString("uid", "");

    // Step 3. Initialize necessary data.
    dataSender = new DataSender(UID);

    // Step 4. Create a listener to handle wifi connectivity.
    network = isDataConnected();/*from  w  w w .  ja  v a  2s  .  co  m*/
    receiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            network = isDataConnected();
            handleWifiChange();
        }
    };
    registerReceiver(receiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));

    // Step 5. Acquire a lock on the CPU for computation during sleep.
    PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakelockTag");
    wakeLock.acquire();

    // Step 6. Begin this service as a foreground service.
    Intent notificationIntent = new Intent(this, Login.class);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    Notification notification = new NotificationCompat.Builder(this)
            .setSmallIcon(android.R.drawable.stat_notify_sync).setContentTitle("Background Service Running")
            .setContentText("Processing data").setContentIntent(pendingIntent).build();

    /*
     * NOTE: A foreground service is used to decouple the service from the application. When a
     * user exits from the application view (the Login activity), using a foreground service
     * prevents this service from restarting. The number supplied below is arbitrary but must be
     * > 0.
     * */
    startForeground(1337, notification);
}

From source file:se.hockersten.timed.vibration.main.CompetitionTab.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    PowerManager pm = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "CompetitionTab.onCreateView()");
    if (savedInstanceState != null) {
        competing = savedInstanceState.getBoolean(COMPETING);
        visible = savedInstanceState.getBoolean(VISIBLE);
        lastPress = (Calendar) savedInstanceState.getSerializable(LAST_PRESS);
    }//  w w w.ja v  a  2  s.c  o  m

    root = inflater.inflate(R.layout.main_competition, container, false);
    return root;
}

From source file:com.almarsoft.GroundhogReader.lib.MessagePosterLib.java

public MessagePosterLib(String currentGroup, String groups, String body, String subject, String references,
        String prevMsgId, Context context) {

    mCurrentGroup = currentGroup;/*from www  .j  a  v a  2  s  .  c o m*/
    mContext = context;
    mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
    mGroups = groups.trim();
    mBody = body;
    mSubject = subject.trim();
    mPostCharset = mPrefs.getString("postCharset", "ISO8859_15");
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE,
            "GroundhogSending");

    // Reply to non-first post in a thread
    if (references != null && references.length() > 0)
        mReferences = references.trim();
    else
        mReferences = null;

    // Reply to a thread
    if (prevMsgId != null && prevMsgId.length() > 0)
        mPrevMsgId = prevMsgId.trim();
    else
        mPrevMsgId = null; // Message starting new thread

    // Reply to the first post in thread
    if (mReferences == null && mPrevMsgId != null) {
        mReferences = mPrevMsgId;
    }

}