“暂停”我的应用程序有效,但不能按照我想要的方式工作

时间:2013-04-19 16:51:15

标签: android

我正在处理一大堆代码,这些代码涉及我的代码会持续一段时间才能使某些东西正常工作。但是,当启动35秒时代码会挂起但是它不能按照我想要的方式工作。 IE我没有得到我想要的输出。

以下是我正在处理的一些代码。

        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);

我试图在系统有机会发射/获取设备的GPS信号后打印出当前的GPS坐标。

关于如何解决这个问题的任何提示都很棒,因为我明白我做错了什么。

1 个答案:

答案 0 :(得分: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();
}

如果有帮助,请告诉我