您好我已经创建了一个服务类,我想获取连续位置(纬度和经度)。 我到底想做的是: 我有活动说A和B.
1.我从Acitivity A调用我的服务类
Intent mIntent=new Intent(A.this, OverlayService.class);
startService(mIntent);
2.在服务中我希望获得连续的纬度和经度并以本地dB存储。这是我的服务。
public class OverlayService extends Service implements LocationListener {
Location location; // location
double previousLat,previousLong;
private static LocationManager _locationManager;
private WindowManager windowManager;
private ImageView chatHead;
private String allocationId, vehicleNumber;
double latitude; // latitude
double longitude; // longitude
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 5; // 5 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 30; // 1 minute
public synchronized static LocationManager getInstance(Context context)
{
if (_locationManager == null)
{
_locationManager = (LocationManager) context
.getSystemService(LOCATION_SERVICE);
}
return _locationManager;
}
@Override
public IBinder onBind(Intent intent) {
// Not used
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("OverlayService", "Service Started");
_locationManager=getInstance(this);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
chatHead = new ImageView(this);
chatHead.setImageResource(R.drawable.backgroundimage);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(200, 400, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
//Drag events can be handled here
chatHead.setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = params.x;
initialY = params.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return true;
case MotionEvent.ACTION_UP:
Intent intent = new Intent(OverlayService.this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
return true;
case MotionEvent.ACTION_MOVE:
params.x = initialX
+ (int) (event.getRawX() - initialTouchX);
params.y = initialY
+ (int) (event.getRawY() - initialTouchY);
windowManager.updateViewLayout(chatHead, params);
return true;
}
return false;
}
});
windowManager.addView(chatHead, params);
this.getSystemService(Context.LOCATION_SERVICE);
_locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,this);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("OverlayService","Inside on Destroy");
if (chatHead != null)
windowManager.removeView(chatHead);
}
@Override
public void onLocationChanged(Location location) {
latitude=location.getLatitude();
longitude=location.getLongitude();
Toast.makeText(getApplicationContext(), "Long: " + location.getLongitude() + ", Lat: " + location.getLatitude(), Toast.LENGTH_SHORT).show();
double currentLat=latitude,currentLong=longitude;
if(currentLat!=0&¤tLong!=0) {
if(previousLat!=currentLat&&previousLong!=currentLong) {
storeInDataBase(null, vehicleNumber, currentLat, currentLong, allocationId);
}else{
Toast.makeText(this,"Not Saving",Toast.LENGTH_SHORT).show();;
}
previousLat=currentLat;
previousLong=currentLong;
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
private void storeInDataBase(DeviceInfo deviceInfo, String vehicleNum, double currentLat, double currentLong, String allocationId) {
final String KEY = "carLatLong" + allocationId;
Constants constants=new Constants();
if (constants.getLatLong(this, KEY) != null&&constants.getLatLong(this, KEY).trim().length()>0) {
String data = constants.getLatLong(this, "carLatLong" + allocationId) + ":" + currentLat + "," + currentLong;
Log.d("Constants", "Data Stored:::::::" + data);
constants.storeLatLong(this, KEY, data);
} else {
String data = currentLat + "," + currentLong;
Log.d("Constants", "Data Stored in else:::::::" + data);
constants.storeLatLong(this, KEY, data);
}
}
}
我在onLocationChanged中获得了很长时间,但这让我有点迟了。有什么我需要改变的吗?还是建议?我想要拉特和长期不断。
答案 0 :(得分:1)
使用 LocationManager.GPS_PROVIDER 代替
_locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,this);
检查this