0

In my application, I've got CommosWare's WakefulIntentservice. From the doWakefulWork method I call a couple of self-written methods. For example:

@Override
protected void doWakefulWork(Intent arg0) {
    exampleMethod();
}

private void exampleMethod() {
    //Stuff
}

It keeps my code clear, but is there a chance of losing the WakeLock when I call self-written methods?

Thanks in advance!

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
Xander
  • 5,487
  • 14
  • 49
  • 77

2 Answers2

2

Calling a method does not mean you leave doWakefulWork and it ends. It's more like copying the method's lines of code to the calling method.

So this here

protected void doWakefulWork(Intent arg0) {
    exampleMethod();

    exampleMethod2();
}

private void exampleMethod() {
    exampleMethod2();
    exampleMethod2();
}

private void exampleMethod2() {
    print("Hello");
}

does exactly the same thing as this here

protected void doWakefulWork(Intent arg0) {
    { // exampleMethod()
        { // exampleMethod2()
            print("Hello");
        }
        { // exampleMethod2()
            print("Hello");
        }
    }

    { // exampleMethod2()
        print("Hello");
    }
}

or without all the braces

protected void doWakefulWork(Intent arg0) {
    print("Hello");
    print("Hello");
    print("Hello");
}

So you never leave doWakefulWork when calling a method and the WakeLock can't go away therefore.

zapl
  • 63,179
  • 10
  • 123
  • 154
  • @AkhilJain I think what I tried to says is that a direct method call can not be asynchronous and could also be written inlined into the calling method. And you don't need "self-written methods" to initiate asynchronous things that outlive the wakelock. – zapl Dec 05 '14 at 14:09
1

So long as the "self-written methods" do not fork a thread or otherwise do something asynchronously, you are safe.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491