WakeLock不工作工作、WakeLock

2023-09-08 09:29:32 作者:伸手摘太阳

我有一个唤醒锁设置,让我仍然可以听到声音的时候,屏幕超时或I preSS的屏幕锁键。从我可以通过网上阅读理解的是,我只需要一个局部唤醒锁。这里是code,但它不能正常工作。没有logcat的错误

 包com.androidsleepmachine.gamble;

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

公共类船舶扩展活动实现View.OnClickListener {
公共静态最终整数[] TIME_IN_MINUTES = {30,45,60,180,360};
公众的MediaPlayer媒体播放器;
公开处理程序处理程序=新的处理程序();
公众按钮按钮2;
公共微调spinner2;
私人PowerManager.WakeLock WL;
//初始化活动
@覆盖
公共无效的onCreate(包savedInstanceState){
    电源管理器PM =(电源管理器)getSystemService(Context.POWER_SERVICE);
    WL = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCKsleeplock);
    super.onCreate(savedInstanceState);

    的setContentView(R.layout.ship);
    按钮2 =(按钮)findViewById(R.id.btn2);
    button2.setOnClickListener(本);
    spinner2 =(微调)findViewById(R.id.spinner2);
    ArrayAdapter<整数GT;适配器=新的ArrayAdapter<整数GT;(这一点,
android.R.layout.simple_spinner_item,TIME_IN_MINUTES);

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

//播放声音,并开始计时
私人无效playSound(INT RESOURCEID){
    //清除任何previous声音文件
    清理(​​);
    //创建一个新的媒体播放器实例和启动
    媒体播放器= MediaPlayer.create(这一点,RESOURCEID);
    mediaPlayer.start();
    //创建计时器停止毫秒x次后声音
    INT selectedTime = TIME_IN_MINUTES [spinner2.getSelectedItemPosition()];
    handler.postDelayed(可运行,selectedTime * 60 * 1000);
}

//手柄按钮回调
@覆盖
公共无效的onClick(视图v){
    开关(v.getId()){
        案例R.id.btn2:
            wl.acquire();
            playSound(R.raw.ocean_ship);
            打破;
    }
}
 保护无效的onStop()
  {

      清理(​​);
      super.onStop();
  }
//停止声音和清理媒体播放器
公共无效清除(){
    如果(媒体播放器!= NULL){
        mediaPlayer.stop();
        mediaPlayer.release();
        媒体播放器= NULL;
         wl.release();
    }
    //取消任何previously运行的任务
    handler.removeCallbacks(可运行);
}

//使用的处理Runnable任务停止声音
市民可运行可运行=新的Runnable(){
    公共无效的run(){
        清理(​​);
    }
};
}
 

解决方案

我会感到惊讶,如果迪维罗实验室的回答解决了这一点,因为Android的文件说,这些额外的国旗做什么用的部分wakelock。

为您正在使用它,它应该工作wakelock很简单:

  PowerManager的PM =(电源管理器)getSystemService(Context.POWER_SERVICE);
WL = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCKsleeplock);
wl.acquire();
//推出
wl.release();
 

请注意,您将需要检查空值,并检查尚未取得,等做这些方法之前,或者你可能会得到一个空指针异常。

我觉得你的问题是,你正在调用清理(),其中包括wakelock释放,所以你是不是真的拿着wakelock。为了确保这一工作(尽管可能会导致电池消耗问题)把你wakelock前期,然后松开,当您完成,在此之前你做别的,你会明显地没有wakelock。

我有一吨的,因为wakelocks不工作的问题,但相信我明确了这个问题到自定义ROM。所以要小心! (不要支持自定义光盘,包括你自己!)

系统探测器 Wake lock detector下载 v1.5.4 汉化版

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();
    }
};
}

解决方案

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!)