requestLocationUpdates

requestLocationUpdates

抱歉,我知道这里发生过此类问题,但是我无法在代码中对其进行操作,我是初学者...
因此,我试图从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

}


因此,对不起您感到愚蠢。有人可以帮我吗?

干杯
一种

最佳答案

我可能发现了问题:我真的不应该在这里调用requestlocationupdates,因为我在定时器类中要求更新。需要去外面测试:)

干杯!

关于java - requestLocationUpdates引发错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32679591/

10-08 22:31