Reputation: 3213
I created my own Java Class to handle the location:
public class MyLocationHandler extends Service implements LocationListener {
......
public Location getLocation() {
try {
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
.......
}
And this is an extract from build.gradle
:
android {
compileSdkVersion 23
buildToolsVersion "24.0.0"
defaultConfig {
applicationId "com.mobile.appoperator"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
PROBLEM:
The problem is that I need to add checkSelfPermission
, but that isn't an activity and plus if I add it, I can't run my app on Android versions older than api lvl 23.
So how can I handle this problem?
Upvotes: 2
Views: 105
Reputation: 5626
I am doing mine inside a service and yes, I currently do not request permission because for some reason, you have to be inside an activity to do that;
Here is what I do instead:
private void updateUserLocation(){
if ( Build.VERSION.SDK_INT >= 23 &&
ContextCompat.checkSelfPermission( getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION )
!= PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission( getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
return;
}
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
LocationListener listener = new GpsListener();
Looper.prepare();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
Looper.loop();
}else {
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
LocationListener listener = new GpsListener();
Looper.prepare();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
Looper.loop();
}
}
}
The best approach to actually request a user to grant you permission is to do this early perhaps in the first activity of the app like Login (then they will see some window explaining why you need GPS enabled or such permission in your app.
If the user rejects the request for permission, then you will have to reroute your app - basically feel sad and do what you would do without location features.
SUMMARY
In general, since you only ask the user to grant the permission perhaps once, you can do that when they login (since after they grant the permission, they will then not be asked again), then when actually getting location updates, you can simply check like I do below and move on!
I hope this helps you get somewhere.
Upvotes: 2