0

I am working on a chunk of code that involves my code holding up for a certain amount of time in order for something to work properly. However the code does hang when the 35 seconds are initiated however its not working the way I want it to. IE I do not get the desired output I want.

Here is some code I am working on.

        LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    LocationListener mlocListener = new MyLocationListener();
    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
            mlocListener);
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            1000L, 1.0f, (LocationListener) this);
    boolean isGPS = locationManager
            .isProviderEnabled(LocationManager.GPS_PROVIDER);

    // Methods to make the code wait 35 seconds
    int time = 35000;
    try {
        Thread.sleep(time);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Intent WriteOut = new Intent(context, WriteOut.class);
    WriteOut.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(WriteOut);

I am trying to print out the current GPS co-ordinates after the system has a chance to launch / acquire the GPS signal of the device.

Any tips on how to pull this off would be wonderful since its clear I am doing something wrong.

4

1 回答 1

0

如果问题是在运行代码之前没有启动服务,那么在运行代码之后调用sleep()Thread无济于事。让它休眠一段时间,同时增加等待时间并检查您需要更改的任何值。我没有弄乱 GPS,所以我不知道你需要检查什么,但我相信你会。

LocationListener mlocListener = new MyLocationListener();

// Methods to make the code wait 35 seconds
int time = 35000;
int curTime = 0;
while (curTime<time && aCoordingateVariable = aDefaultValue)  // I don't mess with GPS so I don't know what you need there but that should give you the idea
try {
        Thread.sleep(100);
        LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
        mlocListener);
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
        1000L, 1.0f, (LocationListener) this);
        boolean isGPS = locationManager
        .isProviderEnabled(LocationManager.GPS_PROVIDER);enter code here
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

让我知道这是否有帮助

于 2013-04-19T17:35:13.937 回答