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

问题描述

我有定期会发出蜂鸣声此应用程序需要运行一个服务(背景)。该手机需要发出蜂鸣声一整天持续5秒,每隔一分钟(在服务中使用的处理器)。我已经实现了这个服务,这是否完美,而是当手机进入深度睡眠模式时,停止执行这一处理程序停止。使用this从这个问题的话,我设法使用唤醒锁接听并能正常工作。但是,当我明确地把手机在深度睡眠模式下,处理器停止执行。我在哪里放置在服务wakelock。低于code段。

 公共类PlaySound延伸服务{
PowerManager.WakeLock WL;
    电源管理器时;
私人SoundManager类mSoundManager;
    布尔wakeUpFlag = FALSE;

@覆盖
    公共无效的onCreate(){
        super.onCreate();
        mSoundManager =新SoundManager类();
        mSoundManager.initSounds(getBaseContext());
        mSoundManager.addSound(1,R.raw.sound);
    }
@覆盖
    公众诠释onStartCommand(意向意图,诠释标志,诠释startId){
        startservice();
        返回START_STICKY;
    }
私人无效startservice(){
        的System.out.println(已启动的服务);
        timer.scheduleAtFixedRate(新的TimerTask(){
            公共无效的run(){
                toastHandler.sendEmptyMessage(0);
            }
        },0,60000);
    }
私人最终处理程序toastHandler =新的处理程序()
    {
        @覆盖
        公共无效的handleMessage(信息MSG)
        {
            结果=启动();

                的System.out.println(结果+结果);
                关闭();
        }
    };

保护无效的close(){
        尝试 {
            如果(wakeUpFlag){
                wl.release();
                的System.out.println(公布的wakelock);
            }

            如果(!pm.isScreenOn()){
                的System.out.println(屏幕关闭 - 回去睡觉);
                pm.goToSleep(1000);
            }
            其他{
                的System.out.println(屏幕上 - 无需睡眠);
            }
            bs.close();
            writer.close();
            的System.out.println(关闭套接字和作家);
            ;内容:System.out.println(+ f.length()/ 1024文件大小)
        }赶上(IOException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }
公共无效启动(){
        尝试{
            wakeUpFlag = FALSE;
            PM =(电源管理器)getSystemService(Context.POWER_SERVICE);


            如果(!pm.isScreenOn()){
                wakeUpFlag = TRUE;
                WL = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE,CollectData);
                的System.out.println(屏幕关闭 - 唤醒锁后天);
                wl.acquire();
            }
            其他{
                的System.out.println(在屏幕上 - 无需唤醒锁);
            }



        }
        赶上(例外五){
            e.printStackTrace();
        }
mSoundManager.playSound(1);
}
 

解决方案

请马克·墨菲提供了模式与 WakefulIntentService 。我建议拿起他的书,不仅为这个类的例子,他包括在其中一人的详细解释,但是对于信息的其他财富,你会发现他们。

我刚刚实现这个模式来我的主要应用程序,这个类的工作就像一个魅力。

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);
}
解决方案

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的服务复发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 11:43