How to detect Location Provider ? GPS or Network Provider

前端 未结 2 1139
野的像风
野的像风 2020-12-18 15:32

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

相关标签:
2条回答
  • 2020-12-18 15:55

    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" />
    
    0 讨论(0)
  • 2020-12-18 15:59

    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

    0 讨论(0)
提交回复
热议问题