This is probably a simple/dumb problem. I can’t find any documentation on it.
I want to create my own LocationProvider in Android. LocationProvider is an abstract class, with no constructor.
import android.location.LocationProvider;
public class provider extends LocationProvider {
public provider() {
}
@Override
public int getAccuracy() {
// TODO Auto-generated method stub
return 0;
}
...
@Override
public boolean supportsSpeed() {
// TODO Auto-generated method stub
return false;
}
}
Eclipse gives me the error here:
“Implicit super constructor LocationProvider() is not visible. Must explicitly invoke another constructor“
But of course the empty super constructor is not visible – there are no constructors for LocationProvider. How am I supposed to create a subclass of an abstract class with no constructor?
For example, this throws the same error:
public abstract class topabstractclass {
private topabstractclass() {
}
}
class mysubclass extends topabstractclass{
mysubclass() {
}
}
I cannot modify Android’s LocationProvider, so I can’t create a constructor that’s public.
Is this just an intrinsically non-subclassible class? Does android just not expect us to ever create LocationProviders? How are other LocationProviders done? Can anyone provide an example?
You are correct in your assumption. It is impossible to extend an abstract class in which all constructors are private. This means, correctly, that Google does NOT want you extending
LocationProvider.First, note that other projects may clone
LocationProvider. They then create their subclasses of it, and can use them as they see fit.Now, I would guess you want to know why
LocationProvideris restricted in such a manner. That is explained by the documentation:Here we have to read between the lines a bit. Consider how much of that information comes directly from the carriers and is phone-specific. This means that tampering with it in your app may stop a device from functioning, since its provider is no longer following its esoteric rules. This is why Android sets up all the providers for us. (This link is really handy to keep around. It will show you how to access that location info without your own provider.)
So, no, you cannot implictly* extend
LocationProvider. You must use one that already exists, because they are specially designed to work between the carrier, the hardware, and the software.*As shown in @Funtik’s answer, you can extend
LocationProviderusing aString, ILocationManagersignature. However, it’s undocumented, marked with{@hide}, and unsupported in previous versions, so you may have to do some dancing around to get the correct construction method.