Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取信息时暂停代码执行_Java_Android_Eclipse_Gps - Fatal编程技术网

Java 获取信息时暂停代码执行

Java 获取信息时暂停代码执行,java,android,eclipse,gps,Java,Android,Eclipse,Gps,有一个工作应用程序,通过GPS收集GPS坐标。 应用程序将在收集后使用这些坐标 但因为获取坐标可能需要10-15秒或更长时间。 应用程序开始获取,在代码中向前移动,获取跳线(通常为lastknowlocation)并使用并非最后一个的坐标。 因此,我经常会收到GPS数据线,如0,0,0或lastknowlocation 我需要以某种方式暂停代码的执行,直到获得新的跳线。 如果有任何想法或帮助,我将不胜感激 public class GPSTracker extends Service imple

有一个工作应用程序,通过GPS收集GPS坐标。 应用程序将在收集后使用这些坐标

但因为获取坐标可能需要10-15秒或更长时间。 应用程序开始获取,在代码中向前移动,获取跳线(通常为lastknowlocation)并使用并非最后一个的坐标。 因此,我经常会收到GPS数据线,如0,0,0或lastknowlocation

我需要以某种方式暂停代码的执行,直到获得新的跳线。 如果有任何想法或帮助,我将不胜感激

public class GPSTracker extends Service implements LocationListener {

private final Context mContext;

// flag for GPS status
boolean isGPSEnabled = false;

// flag for network status
boolean isNetworkEnabled = false;

// flag for GPS status
boolean canGetLocation = false;

Location location; // location
double latitude; // latitude
double longitude; // longitude
Location location_1; // location
Location zadnja; // location

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

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 10 * 1; // 10 seconds

// Declaring a Location Manager
protected LocationManager locationManager;

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

public Location 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
            location = null ;
            locationManager = null;
            this.canGetLocation= false ;
            canGetLocation = false ;


        } else {
            this.canGetLocation = true;
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {



                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");

                    location = locationManager
                            .getLastKnownLocation(LocationManager.GPS_PROVIDER);





                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }

            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

/**
 * Stop using GPS listener Calling this function will stop using GPS in your
 * app
 * */
public void stopUsingGPS() {
    if (locationManager != null) {
        locationManager.removeUpdates(GPSTracker.this);
    }
}

/**
 * Function to get latitude
 * */
public double getLatitude() {
    if (location != null) {
        latitude = location.getLatitude();
    }

    // return latitude
    return latitude;
}

/**
 * Function to get longitude
 * */
public double getLongitude() {
    if (location != null) {
        longitude = location.getLongitude();
    }

    // return longitude
    return longitude;
}

/**
 * Function to check GPS/wifi enabled
 * 
 * @return boolean
 * */
public boolean canGetLocation() {
    return this.canGetLocation;
}

/**
 * Function to show settings alert dialog On pressing Settings button will
 * lauch Settings Options
 * */
public void showSettingsAlert() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

    // Setting Dialog Title
    alertDialog.setTitle("GPS is settings");

    // Setting Dialog Message
    alertDialog
            .setMessage("GPS is not enabled. Do you want to go to settings menu?");

    // On pressing Settings button
    alertDialog.setPositiveButton("Settings",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(
                            Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    mContext.startActivity(intent);
                }
            });

    // on pressing cancel button
    alertDialog.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

    // Showing Alert Message
    alertDialog.show();
}

@Override
public void onLocationChanged(Location location) {
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

}

您需要将GPS集合委托给一个单独的线程,并让主代码监视它。这样一来,对正在发生的事情的管理就在一个线程中,而缓慢的事情就在另一个线程中。这两个线程需要使用经典的状态机技术(同步代码段、信号量等)进行通信。

为什么不在AsyncTask中获取位置,并在AsycTask的PostExecute中设置foundLocation=true类变量,然后用

while(!foundLocation){
 // do something like a loading bar or something
}

发布你认为是问题所在的工作代码。黑客使用Thread.sleep 15秒。但我不能说这是好方法。我只是添加了代码。。一个解决方法是执行While循环,在该循环中,我将循环“if(isGPSEnabled)”部分,并检查lastknownlocation是否更改,并将其与相同的lastknownlocation进行比较。。但是应用程序正在崩溃,这是正常的。或者使用AsyncTask作为方便的包装器,为您处理大部分这些细节。