FileDocCategorySizeDatePackage
CountryDetector.javaAPI DocAndroid 5.1 API5126Thu Mar 12 22:22:30 GMT 2015android.location

CountryDetector

public class CountryDetector extends Object
This class provides access to the system country detector service. This service allows applications to obtain the country that the user is in.

The country will be detected in order of reliability, like

  • Mobile network
  • Location
  • SIM's country
  • Phone's locale

Call the {@link #detectCountry()} to get the available country immediately.

To be notified of the future country change, use the {@link #addCountryListener}

You do not instantiate this class directly; instead, retrieve it through {@link android.content.Context#getSystemService Context.getSystemService(Context.COUNTRY_DETECTOR)}.

Both ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions are needed.

hide

Fields Summary
private static final String
TAG
private final ICountryDetector
mService
private final HashMap
mListeners
Constructors Summary
public CountryDetector(ICountryDetector service)

hide
- hide this constructor because it has a parameter of type ICountryDetector, which is a system private class. The right way to create an instance of this class is using the factory Context.getSystemService.


                                                             
       
        mService = service;
        mListeners = new HashMap<CountryListener, ListenerTransport>();
    
Methods Summary
public voidaddCountryListener(CountryListener listener, android.os.Looper looper)
Add a listener to receive the notification when the country is detected or changed.

param
listener will be called when the country is detected or changed.
param
looper a Looper object whose message queue will be used to implement the callback mechanism. If looper is null then the callbacks will be called on the main thread.

        synchronized (mListeners) {
            if (!mListeners.containsKey(listener)) {
                ListenerTransport transport = new ListenerTransport(listener, looper);
                try {
                    mService.addCountryListener(transport);
                    mListeners.put(listener, transport);
                } catch (RemoteException e) {
                    Log.e(TAG, "addCountryListener: RemoteException", e);
                }
            }
        }
    
public CountrydetectCountry()
Start detecting the country that the user is in.

return
the country if it is available immediately, otherwise null will be returned.

        try {
            return mService.detectCountry();
        } catch (RemoteException e) {
            Log.e(TAG, "detectCountry: RemoteException", e);
            return null;
        }
    
public voidremoveCountryListener(CountryListener listener)
Remove the listener

        synchronized (mListeners) {
            ListenerTransport transport = mListeners.get(listener);
            if (transport != null) {
                try {
                    mListeners.remove(listener);
                    mService.removeCountryListener(transport);
                } catch (RemoteException e) {
                    Log.e(TAG, "removeCountryListener: RemoteException", e);
                }
            }
        }