我想在android中连续跟踪用户位置作为后台服务,我能够使用后台服务和广播接收器实现它,但无法根据可用性找出如何在网络和gps提供商之间切换。
backgroundService.java
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mylistner = new NewLocationListener(getBaseContext());
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setCostAllowed(true);
LocationManager locManager = (LocationManager) getBaseContext()
.getSystemService(Context.LOCATION_SERVICE);
String bestProvider = locManager.getBestProvider(criteria, false);
Toast.makeText(getBaseContext(), "Best Provider "+bestProvider, Toast.LENGTH_SHORT).show();
locManager.requestLocationUpdates(bestProvider, 1000, 5, mylistner);
Toast.makeText(getBaseContext(), "Successfully Called", Toast.LENGTH_SHORT).show();
return START_STICKY;
}
最初bestProvider将有gps或网络,但如果它是gps最初然后如果用户关闭gps,它应该给我使用网络坐标。同样,如果用户打开gps,那么位置提供者应该来自gps。
在服务上有onProviderEnabled和onProviderDisabled方法如何将它们用于此目的
作为堆栈溢出中的许多类似问题的解决方案给出我不希望从开始发送网络和gps提供商我想根据可用性进行切换。 是否可以在服务中实现。 提前致谢答案 0 :(得分:0)
这可以帮助您重新思考问题:
public class GPSTracker extends Service implements android.location.LocationListener {
/**
* ¨PRIVATE ATTRIBUTES
*/
// log
private static final String TAG = GPSTracker.class.getSimpleName();
//
private Context _context;
private static GPSTracker _instance;
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0; // meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 30 * 1; // minute
// Declaring a Location Manager
protected LocationManager _locationManager;
/**
* PUBLIC ATTRIBUTES
*/
boolean _isGPSEnabled = false;
boolean _isNetworkEnabled = false;
boolean _canGetLocation = false;
public Location _location;
double _latitude;
double _longitude;
public GPSTracker() {
_context = null;
}
public static GPSTracker getInstance() {
if (_instance == null) {
_instance = new GPSTracker();
}
return _instance;
}
public void set_context(Context context) {
this._context = context;
}
public Location getLocation(Context context) {
_context = context;
try {
_locationManager = (LocationManager) _context.getSystemService(LOCATION_SERVICE);
_isGPSEnabled = _locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
_isNetworkEnabled = _locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!_isGPSEnabled && !_isNetworkEnabled) {
// no network provider is enabled
}
else {
this._canGetLocation = true;
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) {
// TODO: ERROR getLocation
}
return _location;
}
public void stopUsingGPS() {
if (_locationManager != null) {
_locationManager.removeUpdates(GPSTracker.this);
}
}
public double getLatitude() {
if (_location != null) {
_latitude = _location.getLatitude();
}
return _latitude;
}
public double getLongitude() {
if (_location != null) {
_longitude = _location.getLongitude();
}
return _longitude;
}
/**
* Function to check GPS/wifi enabled
*
* @return boolean
*/
public boolean canGetLocation() {
return this._canGetLocation;
}
/**
* Function to show settings alert dialog On pressing Settings button will lauch Settings Options
*/
public void showSettingsAlert(String title, String msg) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(_context);
alertDialog.setTitle(title);
alertDialog.setMessage(msg);
// Settings button
alertDialog.setPositiveButton("Paramètres", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
_context.startActivity(intent);
}
});
// cancel button
alertDialog.setNegativeButton("Annuler", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// show
alertDialog.show();
}
@Override
public void onLocationChanged(Location location) {
Log.i(TAG, "onLocationChanged--TRACKER--" + location.getLatitude() + " / " + location.getLongitude());
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}