Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wake locks android service recurring

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);
}
like image 407
i_raqz Avatar asked Mar 13 '11 02:03

i_raqz


People also ask

What are the wake locks available in Android?

What are the wake locks available in android Options 1 FULL WAKE LOCK 2 SCREEN BRIGHT WAKE LOCK 3 SCREEN.

What is Partial_wake_lock?

PARTIAL_WAKE_LOCK. Wake lock level: Ensures that the CPU is running; the screen and keyboard backlight will be allowed to go off.

What does wake lock app do?

A wakelock is a powerful concept in Android that allows the developer to modify the default power state of their device. The danger of using a wakelock in an application is that it will reduce the battery life of a device.

How do I enable wake lock permissions?

permission. WAKE_LOCK permission in an <uses-permission> element of the application's manifest. Obtain a wake lock by calling PowerManager#newWakeLock(int, String) . Call acquire() to acquire the wake lock and force the device to stay on at the level that was requested when the wake lock was created.


3 Answers

I dont think you are using the correct flag accorinding to the android documentation fior PowerManager:

*If you hold a partial wakelock, the CPU will continue to run, irrespective of any timers and even after the user presses the power button. In all other wakelocks, the CPU will run, but the user can still put the device to sleep using the power button.

In other words, try using PARTIAL_WAKE_LOCK as this is the only one that gurantees the cpu to run

like image 151
ninjasense Avatar answered Oct 01 '22 15:10

ninjasense


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.

like image 27
Chris Stewart Avatar answered Oct 01 '22 16:10

Chris Stewart


I think you'd be better off using android.app.AlarmManager to schedule a wakeup alarm. Be careful though - you don't want to do any long-running work in your onReceive() method as that's normally called on the main thread, and will hang your activity. You'll still need to acquire the wakelock for the duration of your task to prevent the phone sleeping part-way through.

like image 29
kingrichardthethird Avatar answered Oct 01 '22 16:10

kingrichardthethird