Skip to content

Instantly share code, notes, and snippets.

@sreelallalu
Created November 1, 2017 06:34
Show Gist options
  • Select an option

  • Save sreelallalu/fbe03b17c5e2f1e879f0f8451722603a to your computer and use it in GitHub Desktop.

Select an option

Save sreelallalu/fbe03b17c5e2f1e879f0f8451722603a to your computer and use it in GitHub Desktop.
LocationHelper
public class LocationHelper {
public static final int LOCATION_PERMISION = 555;
public static Location getLocation(Context context) {
try {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNetworkProviderEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isGPSEnabled == false && isNetworkProviderEnabled == false) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(intent);
}
{
if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Toast.makeText(context, "Please Enable App Permissions", Toast.LENGTH_SHORT).show();
// Showsnakbar.Show(context, "Please Enable App Permissions", id);
if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity) context, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISION);
} else {
// Toast.makeText(getApplicationContext(), "Turn On Your GPS", Toast.LENGTH_LONG).show();
// Showsnakbar.Show(context, "Turn On Your GPS", id);
Toast.makeText(context, "urn On Your GPS", Toast.LENGTH_SHORT).show();
}
} else {
// Toast.makeText(getApplicationContext(),"Turn On Your GPS",Toast.LENGTH_LONG).show();
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(intent);
}
// return null;
}
Location locationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location locationNet = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
long GPSLocationTime = 0;
if (null != locationGPS) {
GPSLocationTime = locationGPS.getTime();
}
long NetLocationTime = 0;
if (null != locationNet) {
NetLocationTime = locationNet.getTime();
}
if (0 < GPSLocationTime - NetLocationTime) {
return locationGPS;
} else {
return locationNet;
}
}
}catch (Exception e){
e.printStackTrace();
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment