Skip to content

Instantly share code, notes, and snippets.

@javymarmol
Last active December 15, 2017 16:24
Show Gist options
  • Save javymarmol/2ad599aba775000c976e9fac254a6d17 to your computer and use it in GitHub Desktop.
Save javymarmol/2ad599aba775000c976e9fac254a6d17 to your computer and use it in GitHub Desktop.
private void getLocation() {
try {
if (canGetLocation) {
Log.d(TAG, "Can get location");
if (isGPS) {
// from GPS
Log.d(TAG, "GPS on");
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (loc != null)
//latitude = loc.getLatitude();
//longitude = loc.getLongitude();
Log.w(TAG, "INFO GPS latitude: "+ loc.getLatitude() +", longitude: " + loc.getLongitude());
Toast.makeText(ArrivedConsigneeActivity.this, "INFO GPS latitude: "+ loc.getLatitude() +", longitude: " + loc.getLongitude(), Toast.LENGTH_SHORT).show();
getAddress(loc);
}
} else if (isNetwork) {
// from Network Provider
Log.d(TAG, "NETWORK_PROVIDER on");
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
loc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (loc != null)
//latitude = loc.getLatitude();
//longitude = loc.getLongitude();
Log.w(TAG, "INFO NETWORK latitude: "+ loc.getLatitude() +", longitude: " + loc.getLongitude());
Toast.makeText(ArrivedConsigneeActivity.this, "INFO NETWORK latitude: "+ loc.getLatitude() +", longitude: " + loc.getLongitude(), Toast.LENGTH_SHORT).show();
getAddress(loc);
}
} else {
loc.setLatitude(0);
loc.setLongitude(0);
//latitude = loc.getLatitude();
//longitude = loc.getLongitude();
Log.w(TAG, "latitude: "+ loc.getLatitude() +", longitude: " + loc.getLongitude());
Toast.makeText(ArrivedConsigneeActivity.this, "latitude: "+ loc.getLatitude() +", longitude: " + loc.getLongitude(), Toast.LENGTH_SHORT).show();
}
} else {
Log.d(TAG, "Can't get location");
}
} catch (SecurityException e) {
e.printStackTrace();
}
}
private void getLastLocation() {
try {
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
Log.d(TAG, provider);
Log.d(TAG, location == null ? "NO LastLocation" : location.toString());
} catch (SecurityException e) {
e.printStackTrace();
}
}
private ArrayList findUnAskedPermissions(ArrayList<String> wanted) {
ArrayList result = new ArrayList();
for (String perm : wanted) {
if (!hasPermission(perm)) {
result.add(perm);
}
}
return result;
}
private boolean hasPermission(String permission) {
if (canAskPermission()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return (checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED);
}
}
return true;
}
private boolean canAskPermission() {
return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
}
public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(ArrivedConsigneeActivity.this);
alertDialog.setTitle("GPS is not Enabled!");
alertDialog.setMessage("Do you want to turn on GPS?");
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(ArrivedConsigneeActivity.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
private void captureLocationAction(){
GetLocation TheLocation = new GetLocation(getApplicationContext());
TheLocation.Get_location();
locationManager = (LocationManager) getSystemService(Service.LOCATION_SERVICE);
isGPS = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetwork = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
permissionsToRequest = findUnAskedPermissions(permissions);
if (!isGPS && !isNetwork) {
Log.d(TAG, "Connection off");
showSettingsAlert();
getLastLocation();
} else {
Log.d(TAG, "Connection on");
// check permissions
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (permissionsToRequest.size() > 0) {
requestPermissions(permissionsToRequest.toArray(new String[permissionsToRequest.size()]),
ALL_PERMISSIONS_RESULT);
Log.d(TAG, "Permission requests");
canGetLocation = false;
}
}
// get location
getLocation();
}
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
getLocation();
}
@Override
public void onProviderDisabled(String s) {
if (locationManager != null) {
locationManager.removeUpdates(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment