Explicitly select location providers
Этот коммит содержится в:
@@ -20,16 +20,13 @@ import android.content.Context;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.location.Criteria;
|
|
||||||
import android.location.Location;
|
import android.location.Location;
|
||||||
import android.location.LocationListener;
|
import android.location.LocationListener;
|
||||||
import android.location.LocationManager;
|
import android.location.LocationManager;
|
||||||
import android.os.BatteryManager;
|
import android.os.BatteryManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Looper;
|
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
public class PositionProvider implements LocationListener {
|
public class PositionProvider implements LocationListener {
|
||||||
|
|
||||||
@@ -70,33 +67,20 @@ public class PositionProvider implements LocationListener {
|
|||||||
|
|
||||||
@SuppressLint("MissingPermission")
|
@SuppressLint("MissingPermission")
|
||||||
public void startUpdates() {
|
public void startUpdates() {
|
||||||
try {
|
locationManager.requestLocationUpdates(
|
||||||
locationManager.requestLocationUpdates(
|
getProvider(preferences.getString(MainFragment.KEY_ACCURACY, "medium")),
|
||||||
distance > 0 || angle > 0 ? MINIMUM_INTERVAL : interval, 0,
|
distance > 0 || angle > 0 ? MINIMUM_INTERVAL : interval, 0, this);
|
||||||
getCriteria(preferences.getString(MainFragment.KEY_ACCURACY, "medium")),
|
|
||||||
this, Looper.myLooper());
|
|
||||||
} catch (RuntimeException e) {
|
|
||||||
Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Criteria getCriteria(String accuracy) {
|
public static String getProvider(String accuracy) {
|
||||||
Criteria criteria = new Criteria();
|
|
||||||
switch (accuracy) {
|
switch (accuracy) {
|
||||||
case "high":
|
case "high":
|
||||||
criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
|
return LocationManager.GPS_PROVIDER;
|
||||||
criteria.setPowerRequirement(Criteria.POWER_HIGH);
|
|
||||||
break;
|
|
||||||
case "low":
|
case "low":
|
||||||
criteria.setHorizontalAccuracy(Criteria.ACCURACY_LOW);
|
return LocationManager.PASSIVE_PROVIDER;
|
||||||
criteria.setPowerRequirement(Criteria.POWER_LOW);
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
criteria.setHorizontalAccuracy(Criteria.ACCURACY_MEDIUM);
|
return LocationManager.NETWORK_PROVIDER;
|
||||||
criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
return criteria;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -108,35 +108,31 @@ public class ShortcutActivity extends AppCompatActivity {
|
|||||||
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
||||||
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
|
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
|
||||||
|
|
||||||
Criteria criteria = PositionProvider.getCriteria(
|
String provider = PositionProvider.getProvider(
|
||||||
preferences.getString(MainFragment.KEY_ACCURACY, "medium"));
|
preferences.getString(MainFragment.KEY_ACCURACY, "medium"));
|
||||||
|
|
||||||
try {
|
Location location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
|
||||||
Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, true));
|
if (location != null) {
|
||||||
if (location != null) {
|
sendAlarmLocation(location);
|
||||||
sendAlarmLocation(location);
|
} else {
|
||||||
} else {
|
locationManager.requestSingleUpdate(provider, new LocationListener() {
|
||||||
locationManager.requestSingleUpdate(criteria, new LocationListener() {
|
@Override
|
||||||
@Override
|
public void onLocationChanged(Location location) {
|
||||||
public void onLocationChanged(Location location) {
|
sendAlarmLocation(location);
|
||||||
sendAlarmLocation(location);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStatusChanged(String provider, int status, Bundle extras) {
|
public void onStatusChanged(String provider, int status, Bundle extras) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onProviderEnabled(String provider) {
|
public void onProviderEnabled(String provider) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onProviderDisabled(String provider) {
|
public void onProviderDisabled(String provider) {
|
||||||
}
|
}
|
||||||
}, Looper.myLooper());
|
}, Looper.myLooper());
|
||||||
}
|
|
||||||
} catch (RuntimeException e) {
|
|
||||||
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user