java - requestLocationUpdates 抛出错误

标签 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

}

所以,抱歉我的愚蠢行为。有人可以帮我解决这个问题吗?

干杯

最佳答案

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

干杯!

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

相关文章:

java - Android:EditText导致内存泄漏

java - 运行使用 Maven 构建的 Android 应用程序时出现 NoClassDefFoundError

没有 Activity 的Android Context?和其他无 Activity 编程?

android - 是否可以使用 Google map 在 Android/iOS 上创建转弯 GPS 导航应用程序?

java - 使用 HQL 从它们之间具有一对多关系的连接表中进行选择

Android gradle 在合并时更改资源

java - 指定队列的顺序

java - Android SDK 如何与内部 GPS SDK 对话

java - 如何用java制作沙盒游戏

java - 三个 float 的概率