我在这里搜索了构建在后台服务中运行的有效位置搜索的想法。我找到了这个链接节能 GPS 跟踪,但这不是我想要的。我正在寻找一个简单的解决方案,以米为单位检查距离,然后知道用户是靠近还是远离目标位置,然后节省电池寿命。我不希望时间维度在该算法中仅对距离有效。
注意:我拥有所有正确的权限。这是我的代码(在服务中运行):
public class LocationService extends Service{
private static final int SLEEP = 250;
private static final int VIBRATE = 500;
private double targetLat;
private double targetLng;
private LocationManager manager;
private boolean alarm;
private String ring;
private Uri soundUri;
private long [] pattern;
private float minDistance;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@SuppressWarnings("static-access")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
targetLat = Double.parseDouble(settings.getString("lat", "0"));
targetLng = Double.parseDouble(settings.getString("lng", "0"));
targetLat = targetLat / 1E6;
targetLng = targetLng / 1E6;
alarm = settings.getBoolean("alarm", true);
ring = settings.getString("ringDet", "");
if(ring != ""){
soundUri = Uri.parse(ring);
}
manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, minDistance, location);
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, minDistance, location);
pattern = new long[200];
pattern[0] = 0;
for(int i = 1; i < pattern.length; i++){
if(i % 2 != 0){
pattern[i] = SLEEP;
}
else{
pattern[i] = VIBRATE;
}
}
//return super.onStartCommand(intent, flags, startId);
return super.START_STICKY;
}
LocationListener location = new LocationListener() {
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
@SuppressWarnings("deprecation")
public void onLocationChanged(Location location) {
float [] results = new float [3];
Location.distanceBetween(targetLat, targetLng, location.getLatitude(), location.getLongitude(), results);
float distance = results[0];
if(distance > 20000){
minDistance = 15000;
toaster(">20000");
manager.removeUpdates(this);
}
else if(distance > 10000){
minDistance = 5000;
toaster(">10000");
manager.removeUpdates(this);
}
else if(distance > 5000){
minDistance = 2500;
toaster(">5000");
manager.removeUpdates(this);
}
else if(distance > 2500){
minDistance = 1000;
toaster(">2500");
manager.removeUpdates(this);
}
else if(distance > 1000){
minDistance = 0;
toaster(">1000");
}
if(distance < 800 && alarm){
Notification notification = new Notification(R.drawable.ic_launcher, "WakeApp", System.currentTimeMillis());
Intent notificationIntent = new Intent(getApplicationContext(),MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(), getApplicationContext().getResources().getString(R.string.notification_message), "", contentIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
//notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.icon = R.drawable.ic_launcher;
if(soundUri != null){
notification.sound = soundUri;
}
else if(soundUri == null){
soundUri = RingtoneManager.getActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_ALARM);
notification.sound = soundUri;
}
notification.vibrate = pattern;
alarm = false;
notificationManager.notify(1, notification);
manager.removeUpdates(this);
stopSelf();
}
}
};
private void toaster(String text){
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}