private void buildAlertMessageNoGps() {
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(" Your GPS seems to be disabled, do you want to enable it?")
            .setCancelable(false)
            .setPositiveButton("ّyes", new DialogInterface.OnClickListener() {
                public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                    startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                }
            })
            .setNegativeButton("no", new DialogInterface.OnClickListener() {
                public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                    dialog.cancel();
                }
            });
    final AlertDialog alert = builder.create();
    alert.show();
}
private void turnGPSOff(){
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider.contains("gps")){ //if gps is enabled
    final Intent poke = new Intent();
    poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
    poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
    poke.setData(Uri.parse("3")); 
    sendBroadcast(poke);
}
}
permission in android 6 down
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
and 
permission  android 6  up
 if ( ContextCompat.checkSelfPermission(this, 
 android.Manifest.permission.ACCESS_FINE_LOCATION) ==
 PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this,
 android.Manifest.permission.ACCESS_COARSE_LOCATION) == 
PackageManager.PERMISSION_GRANTED) {
        } else {
        Toast.makeText(this, R.string.error_permission_map, Toast.LENGTH_LONG).show();
    }