How to detect Location Provider ? GPS or Network P

2020-02-11 16:03发布

问题:

I'm using Fused Location Provider for location updates in my app. According to Fused Location Provided documentation, it uses all available resources (e.g. Wifi, GPS, Cell tower, etc..) to optimize location.

But i want to know, is there any way to detect that current location update is from GPS Provider or Network provider, in Location update receiver ?

回答1:

You can check the location provider by location.getProvider() from the location you are getting.

Either you are using fused location api or a previous one, you will get the location from locationManager.getLastKnownLocation or LocationServices.FusedLocationApi.getLastLocation and from location you can get provider by location.getProvider



回答2:

Just few month before Google release the New and simple api for location listener.

It's simple with a few lines of code .

Let me explain

Step 1:Download Google play Service library using SDK manager.Because Location service connect to the Google play.

Step 2: Import library into your work space

Step 3: Add library into your project

step 4: write the following code.

public class MainActivity extends Activity {

    LocationClient mlocationclient;

    Location location;

    LocationRequest mlocationrequest;

    int trasition_type=Geofence.GEOFENCE_TRANSITION_ENTER;

    LocationListener mlistener;

    PendingIntent lviolation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        int isGooglePlayServiceAvilable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
        if (isGooglePlayServiceAvilable == ConnectionResult.SUCCESS) {
        //Create the location client
        mlocationclient=new LocationClient(getApplicationContext(),  new CallBack(), new ConnectError());

        mlocationclient.connect();



        //create the location request objetcs..
        mlocationrequest=LocationRequest.create().setInterval(1000).setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);




        //Crearte the location listener when we want to get location.
        mlistener=new LocationListener() {

            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub


                Log.i("Mine Location", ""+location);

            }
        };

        public class ConnectError implements GooglePlayServicesClient.OnConnectionFailedListener
        {


            @Override
            public void onConnectionFailed(ConnectionResult result) {
                // TODO Auto-generated method stub

            }

        }

        public class CallBack implements GooglePlayServicesClient.ConnectionCallbacks
        {



            @Override
            public void onConnected(Bundle connectionHint) {
                // TODO Auto-generated method stub

                location=mlocationclient.getLastLocation();

                mlocationclient.requestLocationUpdates(mlocationrequest, mlistener);

                //new GetAddress(getApplicationContext()).execute(location);



            }


        }

Step 5: Put this code in android manifest file

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>` 


 <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />