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:org.camlistore.UploadService.java

private void handleUploadAll() {
    startService(new Intent(UploadService.this, UploadService.class));
    final PowerManager.WakeLock wakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
            "Camli Upload All");
    wakeLock.acquire();//  w  w  w  .  j av  a2s . co  m
    Util.runAsync(new Runnable() {
        @Override
        public void run() {
            try {
                List<String> dirs = getBackupDirs();
                List<Uri> filesToQueue = new ArrayList<Uri>();
                for (String dirName : dirs) {
                    File dir = new File(dirName);
                    if (!dir.exists()) {
                        continue;
                    }
                    Log.d(TAG, "Uploading all in directory: " + dirName);
                    File[] files = dir.listFiles();
                    if (files != null) {
                        for (int i = 0; i < files.length; ++i) {
                            File f = files[i];
                            if (f.isDirectory()) {
                                // Skip thumbnails directory.
                                // TODO: are any interesting enough to recurse into?
                                // Definitely don't need to upload thumbnails, but
                                // but maybe some other app in the the future creates
                                // sharded directories. Eye-Fi doesn't, though.
                                continue;
                            }
                            filesToQueue.add(Uri.fromFile(f));
                        }
                    }
                }
                try {
                    service.enqueueUploadList(filesToQueue);
                } catch (RemoteException e) {
                }
            } finally {
                wakeLock.release();
            }
        }
    });
}

From source file:org.ohmage.reminders.types.location.LocTrigService.java

@Override
public void onCreate() {
    //Let the service live forever
    setKeepAliveAlarm(this);

    //Cache the locations
    mLocList = new LinkedList<LocListItem>();
    populateLocList();//from www.  java 2  s . c om

    initState();

    PowerManager powerMan = (PowerManager) getSystemService(POWER_SERVICE);
    mWakeLock = powerMan.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKE_LOCK_TAG);

    if (LocTrigConfig.useNetworkLocation) {
        Log.v(TAG, "LocTrigService: Using network location");
        WifiManager wifiMan = (WifiManager) getSystemService(WIFI_SERVICE);
        mWifiLock = wifiMan.createWifiLock(WifiManager.WIFI_MODE_SCAN_ONLY, TAG);
    }

    if (LocTrigConfig.useMotionDetection) {
        Log.v(TAG, "LocTrigService: Using motion detection");
    }

    mMotionDetectCB = new ILocationChangedCallback.Stub() {

        @Override
        public void locationChanged() throws RemoteException {
            if (LocTrigConfig.useMotionDetection) {
                mHandler.sendMessage(mHandler.obtainMessage());
            }

        }
    };

    mSamplingStarted = false;

    super.onCreate();
}

From source file:org.ohmage.triggers.types.location.LocTrigService.java

@Override
public void onCreate() {
    Log.i(DEBUG_TAG, "LocTrigService: onCreate");

    //Let the service live forever
    setKeepAliveAlarm();/*  w  w  w  .j  a v  a 2 s .  c o m*/

    //Cache the locations
    mLocList = new LinkedList<LocListItem>();
    populateLocList();

    initState();

    PowerManager powerMan = (PowerManager) getSystemService(POWER_SERVICE);
    mWakeLock = powerMan.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKE_LOCK_TAG);

    if (LocTrigConfig.useNetworkLocation) {
        Log.i(DEBUG_TAG, "LocTrigService: Using network location");
        WifiManager wifiMan = (WifiManager) getSystemService(WIFI_SERVICE);
        mWifiLock = wifiMan.createWifiLock(WifiManager.WIFI_MODE_SCAN_ONLY, DEBUG_TAG);
    }

    if (LocTrigConfig.useMotionDetection) {
        Log.i(DEBUG_TAG, "LocTrigService: Using motion detection");
    }

    mMotionDetectCB = new ILocationChangedCallback.Stub() {

        @Override
        public void locationChanged() throws RemoteException {
            if (LocTrigConfig.useMotionDetection) {
                mHandler.sendMessage(mHandler.obtainMessage());
            }

        }
    };

    mSamplingStarted = false;

    super.onCreate();
}

From source file:com.brayanarias.alarmproject.activity.AlarmScreenActivity.java

@Override
protected void onResume() {
    super.onResume();
    Log.e(TAG, "on Resume Method");
    // Set the window to keep screen on
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

    // Acquire wakelock
    PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
    if (wakeLock == null) {
        wakeLock = pm.newWakeLock((PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), tag);
    }//from w ww  .j  a  v a2 s. c  o m

    if (!wakeLock.isHeld()) {
        wakeLock.acquire();
    }
}

From source file:com.ternup.caddisfly.activity.SurveyActivity.java

@Override
protected void onStart() {
    super.onStart();

    if (wakeLock == null || !wakeLock.isHeld()) {
        PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
        wakeLock.acquire();//  ww  w .  j a  v a  2  s. c o m
    }
}

From source file:github.popeen.dsub.service.DownloadService.java

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

    final SharedPreferences prefs = Util.getPreferences(this);
    new Thread(new Runnable() {
        public void run() {
            Looper.prepare();// ww  w  . j  a va  2 s  . c  o  m

            mediaPlayer = new MediaPlayer();
            mediaPlayer.setWakeMode(DownloadService.this, PowerManager.PARTIAL_WAKE_LOCK);

            // We want to change audio session id's between upgrading Android versions.  Upgrading to Android 7.0 is broken (probably updated session id format)
            audioSessionId = -1;
            int id = prefs.getInt(Constants.CACHE_AUDIO_SESSION_ID, -1);
            int versionCode = prefs.getInt(Constants.CACHE_AUDIO_SESSION_VERSION_CODE, -1);
            if (versionCode == Build.VERSION.SDK_INT && id != -1) {
                try {
                    audioSessionId = id;
                    mediaPlayer.setAudioSessionId(audioSessionId);
                } catch (Throwable e) {
                    Log.w(TAG, "Failed to use cached audio session", e);
                    audioSessionId = -1;
                }
            }

            if (audioSessionId == -1) {
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                try {
                    audioSessionId = mediaPlayer.getAudioSessionId();

                    SharedPreferences.Editor editor = prefs.edit();
                    editor.putInt(Constants.CACHE_AUDIO_SESSION_ID, audioSessionId);
                    editor.putInt(Constants.CACHE_AUDIO_SESSION_VERSION_CODE, Build.VERSION.SDK_INT);
                    editor.commit();
                } catch (Throwable t) {
                    // Froyo or lower
                }
            }

            mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mediaPlayer, int what, int more) {
                    handleError(new Exception("MediaPlayer error: " + what + " (" + more + ")"));
                    return false;
                }
            });

            /*try {
               Intent i = new Intent(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION);
               i.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, audioSessionId);
               i.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName());
               sendBroadcast(i);
            } catch(Throwable e) {
               // Froyo or lower
            }*/

            effectsController = new AudioEffectsController(DownloadService.this, audioSessionId);
            if (prefs.getBoolean(Constants.PREFERENCES_EQUALIZER_ON, false)) {
                getEqualizerController();
            }

            mediaPlayerLooper = Looper.myLooper();
            mediaPlayerHandler = new Handler(mediaPlayerLooper);

            if (runListenersOnInit) {
                onSongsChanged();
                onSongProgress();
                onStateUpdate();
            }

            Looper.loop();
        }
    }, "DownloadService").start();

    Util.registerMediaButtonEventReceiver(this);
    audioNoisyReceiver = new AudioNoisyReceiver();
    registerReceiver(audioNoisyReceiver, audioNoisyIntent);

    if (mRemoteControl == null) {
        // Use the remote control APIs (if available) to set the playback state
        mRemoteControl = RemoteControlClientBase.createInstance();
        ComponentName mediaButtonReceiverComponent = new ComponentName(getPackageName(),
                MediaButtonIntentReceiver.class.getName());
        mRemoteControl.register(this, mediaButtonReceiverComponent);
    }

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, this.getClass().getName());
    wakeLock.setReferenceCounted(false);

    WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL, "downloadServiceLock");

    try {
        timerDuration = Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_SLEEP_TIMER_DURATION, "5"));
    } catch (Throwable e) {
        timerDuration = 5;
    }
    sleepTimer = null;

    keepScreenOn = prefs.getBoolean(Constants.PREFERENCES_KEY_KEEP_SCREEN_ON, false);

    mediaRouter = new MediaRouteManager(this);

    instance = this;
    shufflePlayBuffer = new ShufflePlayBuffer(this);
    artistRadioBuffer = new ArtistRadioBuffer(this);
    lifecycleSupport.onCreate();

    if (Build.VERSION.SDK_INT >= 26) {
        Notifications.shutGoogleUpNotification(this);
    }
}

From source file:com.kiandastream.musicplayer.MusicService.java

/**
 * Makes sure the media player exists and has been reset. This will create the media player
 * if needed, or reset the existing media player if one already exists.
 *//*from  w ww  .j av  a  2s  . c om*/
void createMediaPlayerIfNeeded() {
    if (mPlayer == null) {
        mPlayer = new MediaPlayer();
        System.out.println("i am in creating meida player ");
        // Make sure the media player will acquire a wake-lock while playing. If we don't do
        // that, the CPU might go to sleep while the song is playing, causing playback to stop.
        //
        // Remember that to use this, we have to declare the android.permission.WAKE_LOCK
        // permission in AndroidManifest.xml.
        mPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);

        // we want the media player to notify us when it's ready preparing, and when it's done
        // playing:
        mPlayer.setOnPreparedListener(this);
        mPlayer.setOnCompletionListener(this);
        mPlayer.setOnBufferingUpdateListener(this);
        mPlayer.setOnErrorListener(this);
    } else
        mPlayer.reset();
}

From source file:com.horizondigital.delta.UpdateService.java

@SuppressWarnings("deprecation")
@Override/*ww w . j  a v  a2  s  . c o  m*/
public void onCreate() {
    super.onCreate();

    Logger.setDebugLogging(getResources().getBoolean(R.bool.debug_output));

    property_version = getProperty(getString(R.string.property_version), "");
    property_device = getProperty(getString(R.string.property_device), "");
    filename_base = String.format(Locale.ENGLISH, getString(R.string.filename_base), property_version);
    path_base = String.format(Locale.ENGLISH, "%s%s%s%s",
            Environment.getExternalStorageDirectory().getAbsolutePath(), File.separator,
            getString(R.string.path_base), File.separator);
    path_flash_after_update = String.format(Locale.ENGLISH, "%s%s%s", path_base, "FlashAfterUpdate",
            File.separator);
    url_base_delta = String.format(Locale.ENGLISH, getString(R.string.url_base_delta), property_device);
    url_base_update = String.format(Locale.ENGLISH, getString(R.string.url_base_update), property_device);
    url_base_full = String.format(Locale.ENGLISH, getString(R.string.url_base_full), property_device);
    apply_signature = getResources().getBoolean(R.bool.apply_signature);

    Logger.d("property_version: %s", property_version);
    Logger.d("property_device: %s", property_device);
    Logger.d("filename_base: %s", filename_base);
    Logger.d("path_base: %s", path_base);
    Logger.d("path_flash_after_update: %s", path_flash_after_update);
    Logger.d("url_base_delta: %s", url_base_delta);
    Logger.d("url_base_update: %s", url_base_update);
    Logger.d("url_base_full: %s", url_base_full);

    config = Config.getInstance(this);

    wakeLock = ((PowerManager) getSystemService(POWER_SERVICE)).newWakeLock(
            config.getKeepScreenOn() ? PowerManager.SCREEN_DIM_WAKE_LOCK : PowerManager.PARTIAL_WAKE_LOCK,
            "CarbonDelta WakeLock");
    wifiLock = ((WifiManager) getSystemService(WIFI_SERVICE)).createWifiLock(WifiManager.WIFI_MODE_FULL,
            "CarbonDelta WifiLock");

    handlerThread = new HandlerThread("CarbonDelta Service Thread");
    handlerThread.start();
    handler = new Handler(handlerThread.getLooper());

    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    prefs = PreferenceManager.getDefaultSharedPreferences(this);

    scheduler = new Scheduler(this, this);

    networkState = new NetworkState();
    networkState.start(this, this,
            prefs.getInt(PREF_AUTO_UPDATE_NETWORKS_NAME, PREF_AUTO_UPDATE_NETWORKS_DEFAULT));

    batteryState = new BatteryState();
    batteryState.start(this, this, 50, true);

    screenState = new ScreenState();
    screenState.start(this, this);

    prefs.registerOnSharedPreferenceChangeListener(this);

    autoState();
}

From source file:com.classiqo.nativeandroid_32bitz.playback.LocalPlayback.java

private void createMediaPlayerIfNeeded() {
    LogHelper.d(TAG, "createMediaPlayerIfNeeded. needed? ", (mMediaPlayer == null));

    if (mMediaPlayer == null) {
        mMediaPlayer = new MediaPlayer();

        mMediaPlayer.setWakeMode(mContext.getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);

        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setOnErrorListener(this);
        mMediaPlayer.setOnSeekCompleteListener(this);
    } else {/* w  ww . ja v  a  2 s  .  c o m*/
        mMediaPlayer.reset();
    }
}

From source file:com.cityfreqs.littlesirecho.MainActivity.java

private void runScheduler() {
    PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
    wakeLock.acquire();/*from w w  w . j a v  a 2 s.  c o m*/

    scheduler = Executors.newSingleThreadScheduledExecutor();
    scheduler.scheduleAtFixedRate(new Runnable() {
        public void run() {
            logger("executor echo.");
            echoNotification();
        }
    }, userSelectedWaitTime, userSelectedWaitTime, TimeUnit.MILLISECONDS);
}