WakeLock not working

流过昼夜 提交于 2019-12-20 01:45:48

问题


I have a wake lock set up so that I can still hear the sound when the screen times out or I press the screen lock button. From what I can understand through reading online is that I only need a partial wake lock. Here is the code but it is not working. No logcat errors

package com.androidsleepmachine.gamble;

import android.app.Activity;   
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class Ship extends Activity implements View.OnClickListener {
public static final Integer[] TIME_IN_MINUTES = { 30, 45, 60, 180, 360 };
public MediaPlayer mediaPlayer;
public Handler handler = new Handler();
public Button button2;
public Spinner spinner2;
private PowerManager.WakeLock wl;
// Initialize the activity
@Override
public void onCreate(Bundle savedInstanceState) { 
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "sleeplock");
    super.onCreate(savedInstanceState);

    setContentView(R.layout.ship);
    button2 = (Button) findViewById(R.id.btn2);
    button2.setOnClickListener(this);
    spinner2 = (Spinner) findViewById(R.id.spinner2);
    ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,  
android.R.layout.simple_spinner_item, TIME_IN_MINUTES);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);          
    spinner2.setAdapter(adapter); 
}

// Play the sound and start the timer
private void playSound(int resourceId) {
    // Cleanup any previous sound files
    cleanup();
    // Create a new media player instance and start it
    mediaPlayer = MediaPlayer.create(this, resourceId);
    mediaPlayer.start();
    // Create the timer to stop the sound after x number of milliseconds
    int selectedTime = TIME_IN_MINUTES[spinner2.getSelectedItemPosition()];
    handler.postDelayed(runnable, selectedTime * 60 * 1000);
}

// Handle button callback
@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btn2:
            wl.acquire();
            playSound(R.raw.ocean_ship);
            break;
    }
}
 protected void onStop()
  {

      cleanup();
      super.onStop();
  }
// Stop the sound and cleanup the media player
public void cleanup() {
    if (mediaPlayer != null) {
        mediaPlayer.stop();
        mediaPlayer.release();
        mediaPlayer = null;
         wl.release();
    }
    // Cancel any previously running tasks
    handler.removeCallbacks(runnable);
}

// Runnable task used by the handler to stop the sound
public Runnable runnable = new Runnable() {
    public void run() {
        cleanup();
    }
};
}

回答1:


I would be surprised if Di Vero Labs's answer resolved this since Android documentation says those additional flags do nothing with a partial wakelock.

A wakelock is as simple as you are using it, and it should work:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "sleeplock");
wl.acquire();
//release
wl.release(); 

Note that you will need to check for nulls, and check that it is not already acquired, etc before doing these methods or you may get a null pointer exception.

I think your issue is that you are calling cleanup() which includes the wakelock release so you aren't really holding a wakelock. To make sure this works (though might cause battery drain issues) hold your wakelock upfront, then release when you are done, not before you are done else you'll obviously NOT have a wakelock.

I was having a ton of issues because wakelocks weren't working but believe I nailed down the issue to a custom ROM. So beware! (DONT support custom ROMs, including your own!)




回答2:


Try:

wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK |
                PowerManager.ACQUIRE_CAUSES_WAKEUP |
                PowerManager.ON_AFTER_RELEASE, "sleeplock");

I've found the "PowerManager.ACQUIRE_CAUSES_WAKEUP" alleviates a few of the same issues I was having.

Also make sure:

<uses-permission android:name="android.permission.WAKE_LOCK" />

is declared in your manifest.



来源:https://stackoverflow.com/questions/19074074/wakelock-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!