request location permission

This commit is contained in:
whbosm 2018-09-22 13:58:47 +02:00
parent f00148868e
commit a64bd35252
2 changed files with 54 additions and 24 deletions

View file

@ -2,4 +2,5 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
distributionSha256Sum=fa4873ae2c7f5e8c02ec6948ba95848cedced6134772a0169718eadcb39e0a2f
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-bin.zip distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-bin.zip

View file

@ -28,6 +28,7 @@ import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.os.Message; import android.os.Message;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
@ -51,6 +52,7 @@ public class MainActivity extends Activity implements
public static final String userAgent = "OSMfocus"; public static final String userAgent = "OSMfocus";
public static final String PREFS_NAME = "OSMFocusPrefsFile"; public static final String PREFS_NAME = "OSMFocusPrefsFile";
static final int PREFERENCE_REQUEST = 9001; static final int PREFERENCE_REQUEST = 9001;
static final int PERMISSION_REQUEST_ACCESS_FINE_LOCATION = 9002;
static final int INVALIDATE_VIEW = 1000; static final int INVALIDATE_VIEW = 1000;
static final int POLL_NOTIFICATIONS = 1001; static final int POLL_NOTIFICATIONS = 1001;
private static final int LOCATION_INTERVAL = 1000; //ms private static final int LOCATION_INTERVAL = 1000; //ms
@ -413,30 +415,7 @@ public class MainActivity extends Activity implements
super.onResume(); super.onResume();
//locationManager.requestLocationUpdates(mG.mLocProvider, 1000/*ms*/, 1/*meters*/, this); //locationManager.requestLocationUpdates(mG.mLocProvider, 1000/*ms*/, 1/*meters*/, this);
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (mLocationManager != null) { checkAndRequestLocationUpdates();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mLocationManager.addGpsStatusListener(this);
}
Location loc = getMostRecentKnownLocation();
if (loc != null)
this.onLocationChanged(loc);
if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, this);
}
} else if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, this);
}
}
}
if (mG.mUseCompass) { if (mG.mUseCompass) {
sensorManager.registerListener(this, sensorManager.registerListener(this,
@ -488,6 +467,56 @@ public class MainActivity extends Activity implements
return loc; return loc;
} }
private void checkAndRequestLocationUpdates() {
if (mLocationManager != null) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mLocationManager.addGpsStatusListener(this);
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSION_REQUEST_ACCESS_FINE_LOCATION);
}
}
Location loc = getMostRecentKnownLocation();
if (loc != null)
this.onLocationChanged(loc);
if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, this);
}
} else if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, this);
}
}
}
}
@Override
public void onRequestPermissionsResult(int request, String permissions[], int[] granted) {
switch (request) {
case PERMISSION_REQUEST_ACCESS_FINE_LOCATION: {
if (granted.length > 0 && granted[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Location permission granted!", Toast.LENGTH_LONG).show();
checkAndRequestLocationUpdates();
} else {
Toast.makeText(this, "Location permission denied!", Toast.LENGTH_LONG).show();
}
return;
}
}
}
@Override @Override
public void onLocationChanged(Location location) { public void onLocationChanged(Location location) {
mG.mPhyLocation = location; mG.mPhyLocation = location;