唤醒锁定android服务重复出现 [英] Wake locks android service recurring

查看:24
本文介绍了唤醒锁定android服务重复出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this application that needs to run a service (background) that beeps periodically. The phone needs to beep the entire day for 5 seconds every one minute (used a handler in the service). I have implemented this service which does this perfectly, but when the phone goes into deep sleep mode, the execution stops of this handler stops. Using this answer from the question in SO, I managed to use wake locks and it works fine. But when I explicitly put the phone in deep sleep mode, the handler stops executing. Where do I place the wakelock in the service. Code snippet below.

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

推荐答案

按照 Mark Murphy 提供的模式使用 WakefulIntentService.我建议您阅读他的书,不仅是为了详细解释这门课和他包含在其中一个示例中的示例,还为了您可以在其中找到其他丰富的信息.

Follow the pattern Mark Murphy provides with the WakefulIntentService. I would suggest picking up his books, not only for the detailed explanation of this class and example he includes in one of them, but for the other wealth of information you'll find in them.

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

I just recently implemented this pattern for my main app and this class works like a charm.

这篇关于唤醒锁定android服务重复出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆