Search code examples
androidconstructorbroadcastreceiverbootcompleted

Is it possible to override boot complete constructor?


public class bootCompleteReceiver extends BroadcastReceiver{
private External external;

public bootCompleteReceiver(External _external){
    external = _external;
}

@Override
public void onReceive(Context context, Intent intent) {
    external.doStuff();
}
}

With this code boot complete doesn't register, but if you remover the constructor it will work just fine. (note: needless to say, the receiver is properly registered in manifest).

Can anyone verify my fear that static receivers can't have a constructor or explain a way of doing what this code is trying to?


Solution

  • Nothing can call that code on boot as the constructor has a parameter. There is no way to populate that parameter so it can never be called by the system. Your "External" class is an unknown entity as far as the system is concerned.

    Add a parameterless constructor or remove the current constructor so that the system can access it.