Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Network provider and GPS provider working together, need to separate the both based on available providers

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 431
    Answer it

    I am trying to develop an android application that gets the device latitude and longitude using GPS or Network providers, whichever is available for the process. The code works perfectly fine when the device's GPS is on but unfortunately it never works when the device's GPS is switched off.

    I want to get the device's latitude and longitude based on the available providers, for example; if device's GPS is on it should get the device's latitude and longitude using GPS provider and if the GPS is switched off it should get the device's latitude and longitude using the network provider. In my case the network provider always returns false and only works when the device's GPS is switched on where as it should not bother about the device GPS to be on or off it should simply get the latitude and longitude based on network provider.

    I have used the fused API from Google but even that has the same issue, I can only get the latitude and longitude using GPS provider and when I turn off the device's GPS it never returns the required latitude and longitude using network provider because network provider and GPS providers both returns false when device's GPS is switched off.

    Both GPS and network provider is working on device's GPS, I want to separate their process respectively.

    I am using following permissions and features

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.LOCATION_HARDWARE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-feature android:name="android.hardware.location" android:required="true"/>
    <uses-feature android:name="android.hardware.location.network" android:required="true"/>
    <uses-feature android:name="android.hardware.location.gps" android:required="true"/>

    Following is my complete Java code that only works when device's GPS is on, in both GPS and network cases.

    package com.example.administrator.locationgetter;
    
    import android.Manifest;
    import android.app.Activity;
    import android.content.pm.PackageManager;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v4.app.ActivityCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.util.Log;
    import android.view.View;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
        public static final int MY_PERMISSIONS_REQUEST = 0;
    
        LocationManager locationManager;
    
        private LocationListener mLocationListener;
    
    
        private void showStatus(String st)
        {
            Toast.makeText(MainActivity.this,st,Toast.LENGTH_SHORT).show();
        }
        private LocationListener createLocationListener() {
            return new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    // Called when a new location is found by the network location provider.
                    Toast.makeText(MainActivity.this,"accuracy: " + location.getAccuracy() + " lat: " + location.getLatitude() + " lon: " + location.getLongitude(),Toast.LENGTH_SHORT).show();
                }
    
                public void onStatusChanged(String provider, int status, Bundle extras) {
                    showStatus(provider + " -- " + status);
                }
    
                public void onProviderEnabled(String provider) {
                    showStatus(provider + " -- " + "ENABLED!");
                }
    
                public void onProviderDisabled(String provider) {
                    showStatus(provider + " -- " + "DISABLED!");
                }
    
    
            };
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            //setSupportActionBar(toolbar);
    
            mLocationListener = createLocationListener();
            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                }
            });
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
    
            int id = item.getItemId();
    
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    
        @Override
        public void onRequestPermissionsResult(int requestCode,
            String permissions[], int[] grantResults) {
            switch (requestCode) {
                case MY_PERMISSIONS_REQUEST: {
                    // If request is cancelled, the result arrays are empty.
                    if (grantResults.length > 0
                            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
                        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mLocationListener);
    
                    } else {
                        Toast.makeText(MainActivity.this, "So sad! Give me the damn permissions!", Toast.LENGTH_SHORT).show();
    
                    }
                    return;
                }
            }
        }
    
        public void btnLocation(View v) {
    
            if ((ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) || (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED )){
                showStatus("Give me the permissions man!");
                ActivityCompat.requestPermissions(this,
                        new String[]{ Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION},
                        MY_PERMISSIONS_REQUEST);
            }
            else
            {
                showStatus("Already have permission");
                locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
                String locationProvider = LocationManager.NETWORK_PROVIDER;
                Location location = locationManager.getLastKnownLocation(locationProvider);
                if(location != null)
                {
                    Toast.makeText(MainActivity.this,"accuracy: " + location.getAccuracy() + " lat: " + location.getLatitude() + " lon: " + location.getLongitude(),Toast.LENGTH_SHORT).show();
                }
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mLocationListener);
    
            }
        }
        public void btnLocation1(View v) {
    
            if ((ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) || (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED )){
                showStatus("Give me the permissions man!");
                ActivityCompat.requestPermissions(this,
                        new String[]{ Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION},
                        MY_PERMISSIONS_REQUEST);
            }
            else
            {
                showStatus("Already have permission");
                locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
                String locationProvider = LocationManager.GPS_PROVIDER;
                Location location = locationManager.getLastKnownLocation(locationProvider);
                if(location != null)
                {
                    Toast.makeText(MainActivity.this,"accuracy: " + location.getAccuracy() + " lat: " + location.getLatitude() + " lon: " + location.getLongitude(),Toast.LENGTH_SHORT).show();
                }
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
    
            }
        }
    }

    I have tested the application on KitKat, Lollipop and Marshmallow, all have the same results.

    If you download the Baidu maps and launch the app it will ask you to grant the permission for location access, just make sure that your location is turned off and simply grant the location permission to Baidu map you will notice that it will point your location on Baidu maps even if your GPS or location service is off.

 0 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: