应用程序暂停,直到有结果

时间:2016-12-25 18:56:28

标签: android android-asynctask geolocation android-service

我有一个正常工作的函数名称Address(),但它会让我的应用程序暂停,直到有结果。

这是代码

private void Address() {
    gps.getLocation();


    if (gps.canGetLocation == false) {
        // Here you can ask the user to try again, using return; for that
        //Toast.makeText(this, "Your location is not available, please try again.", Toast.LENGTH_SHORT).show();
        return;
    } else {

        address = gps.getLocationAddress();
        street = gps.getLocationArea();
        String[] compare = {"Jawahar Nagar", "Tilak Nagar", "Sector 4", "Adarsh Nagar", "Gurunanakpura",
                "Raja Park", "Janta Colony", "Jhalana Doongri", "Sector 5", "Sector 1", "Sector 2", "Sector 3", "Sector 6", "Sector 7"};
        if (Arrays.asList(compare).contains(street) && address.equals("null")) {
            int zip = Integer.parseInt("302004");
            address = String.valueOf(zip);
            //Toast.makeText(this, "Yea " + address + street, Toast.LENGTH_LONG).show();

        }

    }

}

我尝试使用Asyntask在后台运行以使应用程序快速可靠,但有时它会给出null值。

AsyncTask.execute(new Runnable() {
        @Override
        public void run() {
            Address();
        }
    }); 

我还使另一个类扩展了IntentService,但这总是给出null值。

public class FetchAddressIntentService extends IntentService {

private Intent intent;
String errorMessage = "";
List<Address> addresses = null;
private String TAG = "";
protected ResultReceiver mReceiver;
private GPSTracker gps = new GPSTracker(this);
private String addressText;



public FetchAddressIntentService(String name) {
    super(name);
}

public FetchAddressIntentService() {
    super(null);

}


@Override
protected void onHandleIntent(Intent intent) {

    gps.getLocation();

    if (gps.canGetLocation() == false) {
        Toast.makeText(this, "Can't get your location", Toast.LENGTH_LONG).show();
    } else {
        Geocoder geocoder = new Geocoder(this, Locale.getDefault());
        mReceiver = intent.getParcelableExtra(Local.RECEIVER);


        Location location = intent.getParcelableExtra(
                Local.LOCATION_DATA_EXTRA);

        try {
            addresses = geocoder.getFromLocation(
                    location.getLatitude(),
                    location.getLongitude(),
                    // In this sample, get just a single address.
                    1);
        } catch (IOException ioException) {
            // Catch network or other I/O problems.
            errorMessage = getString(R.string.service_not_available);
            Log.e(TAG, errorMessage, ioException);
        } catch (IllegalArgumentException illegalArgumentException) {
            // Catch invalid latitude or longitude values.
            errorMessage = getString(R.string.invalid_lat_long_used);
            Log.e(TAG, errorMessage + ". " +
                    "Latitude = " + location.getLatitude() +
                    ", Longitude = " +
                    location.getLongitude(), illegalArgumentException);
        }

        // Handle case where no address was found.
        if (addresses == null || addresses.size() == 0) {
            if (errorMessage.isEmpty()) {
                errorMessage = getString(R.string.no_address_found);
                Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
            }
            deliverResultToReceiver(Local.FAILURE_RESULT, errorMessage);
        } else {
            Address address = addresses.get(0);
            /*
            ArrayList<String> addressFragments = new ArrayList<String>();

            for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
                addressFragments.add(address.getAddressLine(i));

            }

            Toast.makeText(this,getString(R.string.address_found),Toast.LENGTH_LONG).show();
            deliverResultToReceiver(Local.SUCCESS_RESULT,
                    TextUtils.join(System.getProperty("line.separator"),
                            addressFragments));
            */

            addressText = String.format(
                    "%s", address.getPostalCode());

            String street = String.format("%s", address.getSubLocality());

            String[] compare = {"Jawahar Nagar", "Tilak Nagar", "Sector 4", "Adarsh Nagar", "Gurunanakpura",
                    "Raja Park", "Janta Colony", "Jhalana Doongri", "Sector 5", "Sector 1", "Sector 2", "Sector 3", "Sector 6", "Sector 7"};

            if (Arrays.asList(compare).contains(street) && address.equals("null")) {
                int zip = Integer.parseInt("302004");
                addressText = String.valueOf(zip);
                intent = new Intent();
                intent.setAction("ZIP");
                intent.putExtra("ZIP",addressText);
                sendBroadcast(intent);

            }

        }
    }


}

private void deliverResultToReceiver(int resultCode, String message) {
    Bundle bundle = new Bundle();
    bundle.putString(Local.RESULT_DATA_KEY, message);
    mReceiver.send(resultCode, bundle);
}
}

0 个答案:

没有答案