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.videolan.vlc.PlaybackService.java

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

    if (!VLCInstance.testCompatibleCPU(this)) {
        stopSelf();// w w  w  .jav  a 2s  .  c  o  m
        return;
    }

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    mDetectHeadset = prefs.getBoolean("enable_headset_detection", true);

    mMediaListPlayer = MediaWrapperListPlayer.getInstance();

    mCallback = new HashMap<IPlaybackServiceCallback, Integer>();
    mCurrentIndex = -1;
    mPrevIndex = -1;
    mNextIndex = -1;
    mPrevious = new Stack<Integer>();
    mEventHandler = EventHandler.getInstance();
    mRemoteControlClientReceiverComponent = new ComponentName(BuildConfig.APPLICATION_ID,
            RemoteControlClientReceiver.class.getName());

    // Make sure the audio 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.
    PowerManager pm = (PowerManager) VLCApplication.getAppContext().getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);

    IntentFilter filter = new IntentFilter();
    filter.setPriority(Integer.MAX_VALUE);
    filter.addAction(ACTION_REMOTE_BACKWARD);
    filter.addAction(ACTION_REMOTE_PLAYPAUSE);
    filter.addAction(ACTION_REMOTE_PLAY);
    filter.addAction(ACTION_REMOTE_PAUSE);
    filter.addAction(ACTION_REMOTE_STOP);
    filter.addAction(ACTION_REMOTE_FORWARD);
    filter.addAction(ACTION_REMOTE_LAST_PLAYLIST);
    filter.addAction(ACTION_WIDGET_INIT);
    filter.addAction(Intent.ACTION_HEADSET_PLUG);
    filter.addAction(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
    filter.addAction(VLCApplication.SLEEP_INTENT);
    filter.addAction(VLCApplication.INCOMING_CALL_INTENT);
    filter.addAction(VLCApplication.CALL_ENDED_INTENT);
    registerReceiver(serviceReceiver, filter);

    final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
    boolean stealRemoteControl = pref.getBoolean("enable_steal_remote_control", false);

    if (!AndroidUtil.isFroyoOrLater() || stealRemoteControl) {
        /* Backward compatibility for API 7 */
        filter = new IntentFilter();
        if (stealRemoteControl)
            filter.setPriority(Integer.MAX_VALUE);
        filter.addAction(Intent.ACTION_MEDIA_BUTTON);
        mRemoteControlClientReceiver = new RemoteControlClientReceiver();
        registerReceiver(mRemoteControlClientReceiver, filter);
    }
    try {
        getPackageManager().getPackageInfo("com.getpebble.android", PackageManager.GET_ACTIVITIES);
        mPebbleEnabled = true;
    } catch (PackageManager.NameNotFoundException e) {
        mPebbleEnabled = false;
    }
}

From source file:co.shunya.gita.player.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.
 */// w w  w  .j a  va 2s. c  o  m
void createMediaPlayerIfNeeded() {
    if (mPlayer == null) {
        mPlayer = new MediaPlayer();

        // 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.setOnErrorListener(this);
    } else
        mPlayer.reset();
}

From source file:com.dzt.musicplay.player.AudioService.java

@Override
public void onCreate() {
    super.onCreate();
    GlobalConstants.print_i(getClass(), "onCreate");
    // Get libVLC instance
    try {/*from   w  w  w  . j  ava 2  s.  co  m*/
        mLibVLC = VLCInstance.getLibVlcInstance(getApplicationContext());
    } catch (LibVlcException e) {
        e.printStackTrace();
    }
    StartLoadFileThread();

    mCallback = new HashMap<IAudioServiceCallback, Integer>();
    mMediaList = new ArrayList<Media>();
    mCurrentIndex = -1;
    mPrevIndex = -1;
    mNextIndex = -1;
    mPrevious = new Stack<Integer>();
    mEventHandler = EventHandler.getInstance();
    mRemoteControlClientReceiverComponent = new ComponentName(getPackageName(),
            RemoteControlClientReceiver.class.getName());

    // Make sure the audio 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.
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);

    IntentFilter filter = new IntentFilter();
    filter.setPriority(Integer.MAX_VALUE);
    filter.addAction(ACTION_REMOTE_BACKWARD);
    filter.addAction(ACTION_REMOTE_PLAYPAUSE);
    filter.addAction(ACTION_REMOTE_PLAY);
    filter.addAction(ACTION_REMOTE_PAUSE);
    filter.addAction(ACTION_REMOTE_STOP);
    filter.addAction(ACTION_REMOTE_FORWARD);
    filter.addAction(ACTION_REMOTE_LAST_PLAYLIST);
    filter.addAction(ACTION_WIDGET_INIT);
    filter.addAction(Intent.ACTION_HEADSET_PLUG);
    filter.addAction(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
    filter.addAction(SLEEP_INTENT);
    registerReceiver(serviceReceiver, filter);

    final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
    boolean stealRemoteControl = pref.getBoolean("enable_steal_remote_control", false);

    if (!LibVlcUtil.isFroyoOrLater() || stealRemoteControl) {
        /* Backward compatibility for API 7 */
        filter = new IntentFilter();
        if (stealRemoteControl)
            filter.setPriority(Integer.MAX_VALUE);
        filter.addAction(Intent.ACTION_MEDIA_BUTTON);
        mRemoteControlClientReceiver = new RemoteControlClientReceiver();
        registerReceiver(mRemoteControlClientReceiver, filter);
    }
    GlobalConstants.print_i(getClass(), "onCreate----end");
}

From source file:at.alladin.rmbt.android.test.RMBTLoopService.java

@Override
public void onCreate() {
    Log.d(TAG, "created");
    super.onCreate();

    partialWakeLock = ((PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE))
            .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "RMBTLoopWakeLock");
    partialWakeLock.acquire();/* w w w . ja  v a2  s.  c o  m*/

    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    readConfig();

    geoLocation = new LocalGeoLocation(this);
    geoLocation.start();

    notificationBuilder = createNotificationBuilder();

    startForeground(NotificationIDs.LOOP_ACTIVE, notificationBuilder.build());
    final IntentFilter actionFilter = new IntentFilter(RMBTService.BROADCAST_TEST_FINISHED);
    actionFilter.addAction(RMBTService.BROADCAST_TEST_ABORTED);
    registerReceiver(receiver, actionFilter);

    final IntentFilter rmbtTaskActionFilter = new IntentFilter(RMBTTask.BROADCAST_TEST_START);
    registerReceiver(rmbtTaskReceiver, rmbtTaskActionFilter);

    final Intent alarmIntent = new Intent(ACTION_ALARM, null, this, getClass());
    alarm = PendingIntent.getService(this, 0, alarmIntent, 0);

    if (ConfigHelper.isLoopModeWakeLock(this)) {
        Log.d(TAG, "using dimWakeLock");
        dimWakeLock = ((PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE))
                .newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP
                        | PowerManager.ON_AFTER_RELEASE, "RMBTLoopDimWakeLock");
        dimWakeLock.acquire();

        final Intent wakeupAlarmIntent = new Intent(ACTION_WAKEUP_ALARM, null, this, getClass());
        wakeupAlarm = PendingIntent.getService(this, 0, wakeupAlarmIntent, 0);

        final long now = SystemClock.elapsedRealtime();
        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, now + 10000, 10000, wakeupAlarm);
    }

    bindService(new Intent(getApplicationContext(), RMBTService.class), this, BIND_AUTO_CREATE);
}

From source file:com.nd.android.u.square.service.MusicPlaybackService.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  w  w .j  av  a 2 s.  co  m*/
void createMediaPlayerIfNeeded() {
    if (mPlayer == null) {
        mPlayer = new MediaPlayer();

        // 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.setOnErrorListener(this);
        mPlayer.setOnBufferingUpdateListener(this);
    } else
        mPlayer.reset();
}

From source file:dk.bearware.gui.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String serverName = getIntent().getStringExtra(ServerEntry.KEY_SERVERNAME);
    if ((serverName != null) && !serverName.isEmpty())
        setTitle(serverName);/* w  w  w.  ja v a  2 s  .  c o m*/
    getActionBar().setDisplayHomeAsUpEnabled(true);

    restarting = (savedInstanceState != null);
    accessibilityAssistant = new AccessibilityAssistant(this);
    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    mediaButtonEventReceiver = new ComponentName(getPackageName(), MediaButtonEventReceiver.class.getName());
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    wakeLock = ((PowerManager) getSystemService(Context.POWER_SERVICE))
            .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
    wakeLock.setReferenceCounted(false);

    channelsAdapter = new ChannelListAdapter(this.getBaseContext());
    filesAdapter = new FileListAdapter(this, this, accessibilityAssistant);
    textmsgAdapter = new TextMessageAdapter(this.getBaseContext(), accessibilityAssistant);
    mediaAdapter = new MediaAdapter(this.getBaseContext());

    // Create the adapter that will return a fragment for each of the five
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setOnPageChangeListener(mSectionsPagerAdapter);

    setupButtons();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        final MediaPlayer mMediaPlayer;
        mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.silence);
        mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                mMediaPlayer.release();
            }
        });
        mMediaPlayer.start();
    }
}

From source file:org.openbmap.services.MasterBrainService.java

/**
 * Acquires wakelock to prevent CPU falling asleep
 *//*  w  w w .j a v  a 2 s . c  om*/
private void requirePowerLock() {
    final PowerManager mgr = (PowerManager) getSystemService(Context.POWER_SERVICE);
    try {
        Log.i(TAG, "Acquiring wakelock " + WAKELOCK_NAME);
        mWakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_NAME);
        mWakeLock.setReferenceCounted(true);
    } catch (final Exception e) {
        Log.e(TAG, "Error acquiring wakelock " + WAKELOCK_NAME + e.toString(), e);
    }
}

From source file:dk.nota.lyt.libvlc.PlaybackService.java

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

    mSettings = PreferenceManager.getDefaultSharedPreferences(this);
    mMediaPlayer = newMediaPlayer();//w  w  w .  j av a 2 s. co  m
    if (!Utils.testCompatibleCPU(this)) {
        stopSelf();
        return;
    }

    mDetectHeadset = mSettings.getBoolean("enable_headset_detection", true);

    mCurrentIndex = -1;
    mPrevIndex = -1;
    mNextIndex = -1;
    mPrevious = new Stack<Integer>();
    mRemoteControlClientReceiverComponent = new ComponentName(this.getApplicationContext(),
            RemoteControlEventReceiver.class.getName());

    // Make sure the audio 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 stopService.
    PowerManager pm = (PowerManager) this.getApplicationContext().getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);

    IntentFilter filter = new IntentFilter();
    filter.setPriority(Integer.MAX_VALUE);
    filter.addAction(ACTION_REMOTE_BACKWARD);
    filter.addAction(ACTION_REMOTE_PLAYPAUSE);
    filter.addAction(ACTION_REMOTE_PLAY);
    filter.addAction(ACTION_REMOTE_PAUSE);
    filter.addAction(ACTION_REMOTE_STOP);
    filter.addAction(ACTION_REMOTE_FORWARD);
    filter.addAction(Intent.ACTION_HEADSET_PLUG);
    filter.addAction(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
    registerReceiver(mReceiver, filter);
}

From source file:com.quran.labs.androidquran.service.AudioService.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 ww  w. j  a va 2 s  .c  om
 */
private void createMediaPlayerIfNeeded() {
    if (mPlayer == null) {
        mPlayer = new MediaPlayer();

        // 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.setOnErrorListener(this);
        mPlayer.setOnSeekCompleteListener(this);
    } else
        mPlayer.reset();
}

From source file:org.awesomeapp.messenger.service.RemoteImService.java

@Override
public void onCreate() {
    debug("ImService started");

    final String prev = Debug.getTrail(this, SERVICE_CREATE_TRAIL_KEY);
    if (prev != null)
        Debug.recordTrail(this, PREV_SERVICE_CREATE_TRAIL_TAG, prev);
    Debug.recordTrail(this, SERVICE_CREATE_TRAIL_KEY, new Date());
    final String prevConnections = Debug.getTrail(this, CONNECTIONS_TRAIL_TAG);
    if (prevConnections != null)
        Debug.recordTrail(this, PREV_CONNECTIONS_TRAIL_TAG, prevConnections);
    Debug.recordTrail(this, CONNECTIONS_TRAIL_TAG, "0");

    mConnections = new Hashtable<Long, ImConnectionAdapter>();
    mConnectionsByUser = new Hashtable<String, ImConnectionAdapter>();

    mHandler = new Handler();

    Debug.onServiceStart();//  ww w.ja  va 2s  .com

    //startForegroundCompat();

    PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "IM_WAKELOCK");

    // Clear all account statii to logged-out, since we just got started and we don't want
    // leftovers from any previous crash.
    clearConnectionStatii();

    mStatusBarNotifier = new StatusBarNotifier(this);
    mServiceHandler = new ServiceHandler();

    mPluginHelper = ImPluginHelper.getInstance(this);
    mPluginHelper.loadAvailablePlugins();

    // Have the heartbeat start autoLogin, unless onStart turns this off
    mNeedCheckAutoLogin = true;

    HeartbeatService.startBeating(getApplicationContext());

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
    startForeground(notifyId, getForegroundNotification());

}