One way of obtaining this is to use PASSIVE_PROVIDER
, which as stated here:
A special location provider for receiving locations without actually initiating a location fix.
This provider can be used to passively receive location updates when
other applications or services request them without actually
requesting the locations yourself. This provider will return locations
generated by other providers. You can query the getProvider() method
to determine the origin of the location update. Requires the
permission ACCESS_FINE_LOCATION, although if the GPS is not enabled
this provider might only return coarse fixes.
Constant Value: "passive"
One certain way of obtaining what you want is the following:
ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
State mobile = conMan.getNetworkInfo(0).getState();
State wifi = conMan.getNetworkInfo(1).getState();
After that, you distinguish them by:
if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) {
//mobile
} else if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
//wifi
}
Check, if the mobile or wifi is connected, if wifi - then use PASSIVE_PROVIDER
, if mobile - use NETWORK_PROVIDER
.
Cheers :)