質問

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 LocationProvider is restricted in such a manner. That is explained by the documentation:

Each provider has a set of criteria under which it may be used; for example, some providers require GPS hardware and visibility to a number of satellites; others require the use of the cellular radio, or access to a specific carrier's network, or to the internet. They may also have different battery consumption characteristics or monetary costs to the user. The Criteria class allows providers to be selected based on user-specified criteria.

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 LocationProvider using a String, ILocationManager signature. 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.

他のヒント

Are you talking about android.location.LocationProvider ?

According to grepcode.com it has a public constructor public LocationProvider(String name, ILocationManager service) You could try it.

You're right in that you cannot inherit from a class whose constructors are all private. Normally I'd expect them to be package-private only, so that other classes in the same package, presumably written by its implementors, can still inherit from it.

However, on Android 4.1.1, it looks like LocationProvider does have a public constructor. Its signature suggests that you should implement ILocationManager instead.

Going back through versions, this constructor was added in 2.3. Version 2.2.2 has one that takes just a String. That constructor goes all the way back to 1.6.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top