Last active
February 8, 2020 07:47
-
-
Save esabook/2ec5e4b173101315141c69ed00a5a55c to your computer and use it in GitHub Desktop.
GPS location init for android
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 ***.utils; | |
import android.Manifest; | |
import android.annotation.SuppressLint; | |
import android.app.Activity; | |
import android.app.AlertDialog; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.pm.PackageManager; | |
import android.location.Criteria; | |
import android.location.Location; | |
import android.location.LocationListener; | |
import android.location.LocationManager; | |
import android.os.Bundle; | |
import android.provider.Settings; | |
import android.support.v4.app.ActivityCompat; | |
import ***.R; | |
public class __LocationProvider { | |
static Activity ctx; | |
static LocationManager locationManager; | |
static Location location; | |
static boolean shouldRequestEnablingLocationService = true; | |
static AlertDialog mDialog; | |
/** | |
* @return | |
*/ | |
public static Location getLocation() { | |
if (locationManager == null || location == null) initLocationManager(); | |
return location; | |
} | |
/** | |
* @param context | |
*/ | |
@SuppressLint("MissingPermission") | |
public static synchronized void init(Activity context) { | |
__LocationProvider.ctx = context; | |
} | |
private static void initLocationManager() { | |
locationManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE); | |
if (ActivityCompat.checkSelfPermission(ctx, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED | |
&& ActivityCompat.checkSelfPermission(ctx, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { | |
locationManager.requestLocationUpdates(__LocationProvider.getBestProvider(), 0, 0, getListener()); | |
location = locationManager.getLastKnownLocation(getBestProvider()); | |
} else { | |
ActivityCompat.requestPermissions( | |
ctx, | |
new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, | |
0); | |
} | |
} | |
/** | |
* | |
*/ | |
protected static void showPermissionDialog() { | |
if (ctx == null || !shouldRequestEnablingLocationService) return; | |
if (!ctx.getWindow().getDecorView().getRootView().isShown()) return; | |
final AlertDialog.Builder dialog = new AlertDialog.Builder(__GlobalApp.getActivity()).setCancelable(true); | |
String msg = ctx.getResources() | |
.getString(R.string.help_us_to_provide_a_fast_service); | |
String buttonPos = ctx.getResources() | |
.getString(R.string.location_setting); | |
String buttonNeg = ctx.getResources().getString(android.R.string.cancel); | |
dialog.setMessage(msg) | |
.setPositiveButton(buttonPos, (paramDialogInterface, paramInt) -> { | |
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); | |
myIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); | |
ctx.startActivity(myIntent); | |
}) | |
.setNegativeButton(buttonNeg, (paramDialogInterface, paramInt) -> { | |
shouldRequestEnablingLocationService = false; | |
}); | |
if (mDialog == null || !mDialog.isShowing()) | |
mDialog = dialog.show(); | |
} | |
/** | |
* @return | |
*/ | |
protected static LocationListener getListener() { | |
return new LocationListener() { | |
@Override | |
public void onLocationChanged(Location loc) { | |
__LocationProvider.location = loc; | |
} | |
@Override | |
public void onStatusChanged(String provider, int status, Bundle extras) { | |
} | |
@Override | |
public void onProviderEnabled(String provider) { | |
} | |
@Override | |
public void onProviderDisabled(String provider) { | |
showPermissionDialog(); | |
} | |
}; | |
} | |
/** | |
* @return | |
*/ | |
public static String getBestProvider() { | |
Criteria criteria = new Criteria(); | |
criteria.setAccuracy(Criteria.ACCURACY_FINE); | |
criteria.setAltitudeRequired(false); | |
criteria.setBearingRequired(false); | |
criteria.setCostAllowed(true); | |
criteria.setPowerRequirement(Criteria.POWER_LOW); | |
String provider = locationManager.getBestProvider(criteria, false); | |
return provider == null ? LocationManager.PASSIVE_PROVIDER : provider; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment