Search code examples
androidalarmwakelock

Calling WakefulIntentService.sendWakefulWork causes runtimeexception


When this code is run, it gives me a exception RuntimeException. I have yet to add any code to the WakeWork.class. Any thoughts on why this is bombing out?

THIS IS CALLED FROM MY 'ON RECEIVER' ALARM.

   Intent i = new Intent(context, WakeWork.class);
   WakefulIntentService.sendWakefulWork(context, i);

manifest.xml
<service android:name="com.PageP.WakeWork"></service>

WAKEWORK.CLASS

package com.PageP;

import android.content.Intent;

public class WakeWork extends WakefulIntentService {

public WakeWork(String name) {
    super(name);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(){
    super.onCreate();
}

@Override
protected void doWakefulWork(Intent intent) {
    // TODO Auto-generated method stub

}
}

ERROR

    Thread [<1> main] (Suspended (exception RuntimeException))  
        ActivityThread.handleCreateService(ActivityThread$CreateServiceData) line: 2346 
        ActivityThread.access$1600(ActivityThread, ActivityThread$CreateServiceData) line: 126  
        ActivityThread$H.handleMessage(Message) line: 1221  
        ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
        Looper.loop() line: 137 
        ActivityThread.main(String[]) line: 4560    
        Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
        Method.invoke(Object, Object...) line: 511  
        ZygoteInit$MethodAndArgsCaller.run() line: 784  
        ZygoteInit.main(String[]) line: 551 
        NativeStart.main(String[]) line: not available [native method]  

LONGCAT

04-22 10:32:04.127: W/AsyncTask(9004): com.PageP.GrabURL.doInBackground(GrabURL.java:36) 04-22 10:32:04.127: W/AsyncTask(9004): at com.PageP.GrabURL.doInBackground(GrabURL.java:1) 04-22 10:32:19.080: E/AndroidRuntime(9004): java.lang.RuntimeException: Unable to instantiate service com.PageP.WakeWork: java.lang.InstantiationException: can't instantiate class com.PageP.WakeWork; no empty constructor 04-22 10:32:19.080: E/AndroidRuntime(9004): Caused by: java.lang.InstantiationException: can't instantiate class com.PageP.WakeWork; no empty constructor


Solution

  • Change:

    public WakeWork(String name) {
        super(name);
        // TODO Auto-generated constructor stub
    }
    

    to:

    public WakeWork() {
        super("George");
    }
    

    (feel free to substitute in some other value for the name)

    As the stack trace shows you, Android cannot instantiate your service because there is no zero-argument public constructor, which is what the above fix addresses.