requestLocationUpdates抛出错误

时间:2015-09-20 12:49:23

标签: java android gps location timertask

抱歉,我知道这里发生过这样的问题,但我不能在我的代码中操作它们,我是初学者...... 所以,我试图从LocationManager读取一个GPS坐标,我的代码抛出一个"无法在线程中创建处理程序,而该线程没有调用Looper.prepare()"。

所以我的代码就这样工作了。我使用了Timer和Timertask类来创建一个计划任务,从中读取我的坐标。

这是一个计时器类:

public class GeoLocationTimer extends Timer {
private List coords;
private Context context;

public GeoLocationTimer(Context context){
    this.context = context;
    this.coords = new ArrayList<Double>();
    //Log.e("cont timer","content" + context);
}

public void addPosition(Double pos) {
    this.coords.add(pos);
}

public void scheduleTasks(long interval) {
    //Log.e("z schedule","cont"+context);
    this.schedule(new GeoLocationTask(this, context), 0, interval);
}

public void cancelTasks() {
    this.cancel();
}

public List getList(){
    return coords;
}

这是任务:

public class GeoLocationTask extends TimerTask{
private final GeoLocationTimer timerContext;
private final Context context;
private Pair<Double, Double> coordsSet;


public GeoLocationTask(GeoLocationTimer timerContext, Context context){
    this.timerContext = timerContext;
    this.context = context;
}

@Override
public void run() {
    // TODO Auto-generated method stub
    GeoActivity tracker = new GeoActivity(context);
    coordsSet = tracker.getLocation();
    Log.e("first","timertask");
    if (coordsSet != null){
    Log.e("first","a tu wartosc" + coordsSet.first);
    Log.e("second","a tu wartosc" + coordsSet.second);

    timerContext.addPosition(coordsSet.first);
    timerContext.addPosition(coordsSet.second);
    //context.addPosition(tracker.getLocationNow().get(1));
    }
}
public boolean cancel() {
    return false;
}

}

以下是我尝试运行此任务的上下文:

package com.example.gpstracking;

public class GeoActivity extends ContextWrapper {
Context context;
public GeoActivity(Context base) {
    super(base);
    this.context = base;
}

public Pair<Double, Double> getLocation(){

    Tracking track = new Tracking(context);
    return track.getLocation();
}

现在跟踪:

public class Tracking extends Service implements LocationListener{
private final Context mContext;
Location location; // location
double latitude; // latitude
double longitude; // longitude

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 30 * 1; // 0.5 minute

// flag for GPS status
boolean isGPSEnabled = false;

// flag for network status
boolean isNetworkEnabled = false;
boolean canGetLocation = false;

protected LocationManager locationManager;

public Tracking(Context context) {
    this.mContext = context;
}


public Pair<Double, Double> getLocation() {
try {
    locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

    // getting GPS status
    isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

    // getting network status
    isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    if (!isGPSEnabled && !isNetworkEnabled) {
        // no network provider is enabled
        Log.e("no provider","turn it on man!");
    } else {
        this.canGetLocation = true;
        // First get location from Network Provider
        if (isNetworkEnabled) {
            Log.e("Network", "Network");
                location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                latitude = location.getLatitude();
                longitude = location.getLongitude();
                Log.e("latitude","latitude"+latitude);
                Log.e("longitude","longitude"+longitude);
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                return new Pair(latitude,longitude);


        }
        // if GPS Enabled get lat/long using GPS Services
        if (isGPSEnabled) {
            Log.e("GPS", "GPS");
                locationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                longitude = location.getLongitude();
                return new Pair(latitude,longitude);


        }
    }

} catch (Exception e) {
    Log.e("excepton:","exp" + e.getMessage());
    e.printStackTrace();
    e.getMessage();
}

return new Pair(0.0,0.0);
}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

}

所以,抱歉愚蠢。有人可以帮我这个吗?

干杯 甲

0 个答案:

没有答案