Monday, April 25, 2016

Check if GPS/Network Provider Services are enabled or not?


How to check if GPS/Network Provider Services are enabled?


LocationManager lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
boolean gps_enabled = false;
boolean network_enabled = false;

try {
    gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch(Exception ex) {}

try {
    network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch(Exception ex) {}

if(!gps_enabled && !network_enabled) {
    // notify user
 AlertDialog.Builder dialog = new AlertDialog.Builder(context);
 dialog.setMessage(context.getResources().getString(R.string.gps_network_not_enabled));
 dialog.setPositiveButton(context.getResources().getString(R.string.open_location_settings), 
new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface paramDialogInterface, int paramInt) {
           // TODO Auto-generated method stub
           Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
           context.startActivity(myIntent);
             //get gps
            }
        });
    dialog.setNegativeButton(context.getString(R.string.Cancel), new DialogInterface.
OnClickListener()
   {

            @Override
            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                // TODO Auto-generated method stub

            }
        });
    dialog.show();      
}


Add below permissions in androidManifest.xml 

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

//-----------------------------------------------------------------------
NOTE:-if you got any warning message like "Call requires permission which may be rejected
 by user. Code should explicitly check to see if permission is available." then you can 
try one by one of below step.


1) Ensure you have your permissions listed in the Manifest:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
(2) Ensure you use ContextCompat as this has compatibility with older API levels.
(3) In your location service, or class that initializes your LocationManager and gets the last known location, we need to check the permissions:
if ( Build.VERSION.SDK_INT >= 23 &&
             ContextCompat.checkSelfPermission( context, android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED &&
             ContextCompat.checkSelfPermission( context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return  ;
        }

(4) Ensure you request permissions from the user: 
if ( ContextCompat.checkSelfPermission( this, android.Manifest.permission.ACCESS_COARSE_
LOCATION ) != PackageManager.PERMISSION_GRANTED ) {

     ActivityCompat.requestPermissions( this, new String[] {  android.Manifest.permission.ACCESS_COARSE_LOCATION  },
                                                LocationService.MY_PERMISSION_ACCESS_COURSE_LOCATION );
 }



3 comments:

  1. Hey Thanks for this post. It will help a lot of people here.
    At the same time you can check our website also as we build app for your business. If you are a startup and confused about which business you should pursue in future, you can contact us. Our top product is Uber Clone Script .
    Uber Clone App .
    Uber Clone .

    ReplyDelete