7

我有这个应用程序需要运行定期发出哔哔声的服务(后台)。电话需要每隔一分钟发出 5 秒的哔声(使用服务中的处理程序)。我已经实现了这个完美的服务,但是当手机进入深度睡眠模式时,这个处理程序的执行停止。使用SO 中问题的这个答案,我设法使用唤醒锁,它工作正常。但是当我明确地将手机置于深度睡眠模式时,处理程序停止执行。我在哪里将唤醒锁放在服务中。下面的代码片段。

public class PlaySound extends Service{
PowerManager.WakeLock wl ;
    PowerManager pm;
private SoundManager mSoundManager;
    boolean wakeUpFlag = false;

@Override
    public void onCreate(){
        super.onCreate();
        mSoundManager = new SoundManager();
        mSoundManager.initSounds(getBaseContext());
        mSoundManager.addSound(1, R.raw.sound);
    }
@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        startservice();
        return START_STICKY;
    }
private void startservice() {
        System.out.println("Started the service");
        timer.scheduleAtFixedRate( new TimerTask() {
            public void run() {
                toastHandler.sendEmptyMessage(0);
            }
        }, 0, 60000);
    }
private final Handler toastHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            result =start();

                System.out.println("result"+result);
                close();
        }
    };

protected void close() {
        try {
            if(wakeUpFlag){
                wl.release();
                System.out.println("Released the wakelock");
            }

            if(!pm.isScreenOn()){
                System.out.println("Screen is off - back to sleep");
                pm.goToSleep(1000);
            }
            else{
                System.out.println("Screen is on - no need to sleep");
            }
            bs.close();
            writer.close();
            System.out.println("Closed socket and writer");
            System.out.println("Size of file:"+f.length()/1024);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
public void start(){
        try{
            wakeUpFlag = false;
            pm = (PowerManager)getSystemService(Context.POWER_SERVICE);


            if(!pm.isScreenOn()) {
                wakeUpFlag  = true;
                wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE,"CollectData");
                System.out.println("Screen off - wake lock acquired");
                wl.acquire();
            }
            else{
                System.out.println("Screen on - no need of wake lock");
            }



        }
        catch(Exception e){
            e.printStackTrace();
        }
mSoundManager.playSound(1);
}
4

3 回答 3

5

我不认为您使用的是符合 android 文档 fior PowerManager的正确标志:

*如果您持有部分唤醒锁,CPU 将继续运行,不管任何定时器,甚至在用户按下电源按钮后。在所有其他唤醒锁中,CPU 将运行,但用户仍然可以使用电源按钮使设备进入睡眠状态。

换句话说,尝试使用PARTIAL_WAKE_LOCK,因为这是保证 CPU 运行的唯一方法

于 2011-03-13T05:18:19.730 回答
3

遵循 Mark Murphy 通过WakefulIntentService提供的模式。我建议拿起他的书,不仅是为了详细解释这门课和他在其中一本书中包含的例子,而且为了你会在其中找到的其他丰富信息。

我最近刚刚为我的主应用程序实现了这个模式,这个类就像一个魅力。

于 2011-03-13T04:48:02.863 回答
1

我认为您最好使用 android.app.AlarmManager 来安排唤醒警报。不过要小心——你不想在你的 onReceive() 方法中做任何长时间运行的工作,因为这通常在主线程上调用,并且会挂起你的活动。您仍然需要在任务期间获取唤醒锁,以防止手机在中途休眠。

于 2011-10-29T10:01:41.747 回答