在Android中的Gps跟踪

时间:2013-12-04 16:28:57

标签: android gps

我正在尝试编写跟踪我的gps坐标的应用程序。 每隔10秒我想将我的坐标发送到服务器 - 对于我使用AlarmManager。 为了获取坐标我正在使用实现onClickListener的Service。 我如何开始服务:

 public void startAlarm() {
    Intent intent = new Intent(this, GpsService.class);
    PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, 0,
            10 * 1000, pintent);
}

我的gps服务:

public class GpsService extends Service implements LocationListener {
// Declaring a Location Manager
private LocationManager locationManager;

Location location; // location
double latitude; // latitude
double longitude; // longitude
double accuracy;
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 10 * 1; //10secs

@Override
public void onCreate() {
    super.onCreate();
    if(locationManager == null ) {
    locationManager = (LocationManager) this
                .getSystemService(LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,
            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
    }
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    getLocation();
    if (location != null) {
        //send to server
    }
    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
    locationManager.removeUpdates(this);
    locationManager = null;
    super.onDestroy();
}

@Override
public void onLocationChanged(Location location) {
    Log.v("myLogs", "GetLocation service: ONLOCATIONCHANGED");
    this.location = location;
}
public Location getLocation() {
    try {
        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
        if (!isGPSEnabled ) {
            stopSelf();
        } else {
            this.canGetLocation = true;
            if (isGPSEnabled) {
                if (location == null) {
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                        }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return location;
}

}

它在模拟器上运行完美,但在真实设备上它只显示GPS查找图标。有什么问题?

0 个答案:

没有答案