Created
March 28, 2017 08:44
-
-
Save m7mdra/6d5134b307803640dc9d2b43d9169d49 to your computer and use it in GitHub Desktop.
checks gps availability and show #AlertDialg when its not
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package util; | |
import android.app.Activity; | |
import android.app.AlertDialog; | |
import android.content.Context; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import android.os.Build; | |
import android.provider.Settings; | |
import android.text.TextUtils; | |
import io.m7mdra.rickshaw.R; | |
/** | |
* Created by allaeem on 3/27/2017. | |
*/ | |
public class LocationUtils { | |
public static final int REQUEST_CODE = 2131; | |
public static boolean isLocationEnabled(Context context) { | |
int locationMode; | |
String locationProviders; | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){ | |
try { | |
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE); | |
} catch (Settings.SettingNotFoundException e) { | |
e.printStackTrace(); | |
return false; | |
} | |
return locationMode != Settings.Secure.LOCATION_MODE_OFF; | |
}else{ | |
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); | |
return !TextUtils.isEmpty(locationProviders); | |
} | |
} | |
public static void openLocationSettings(final Activity activity) { | |
AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity); | |
alertDialog.setTitle(R.string.gps_settings_title); | |
alertDialog.setMessage(R.string.enable_gps); | |
alertDialog.setPositiveButton(activity.getString(R.string.ok), new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int which) { | |
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); | |
activity.startActivityForResult(intent, REQUEST_CODE); | |
dialog.cancel(); | |
} | |
}); | |
alertDialog.setNegativeButton(activity.getString(R.string.cancel), new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int which) { | |
dialog.cancel(); | |
} | |
}); | |
alertDialog.show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment