reverse geocoding getPostalCode on Launch

Put your problem here if it does not fit any of the other categories.

reverse geocoding getPostalCode on Launch

Postby M4tchB0X3r » Mon Feb 18, 2013 12:48 am

I’m an experienced AS3 developer and I’ve done quite some stuff with Java for my backends but I’m new to Native Android development so I’m having troubles with some basic Tasks for my first Project.
So hope one of you cracks can help me out here or point me in the right directions, it would be much appreciated. That briefly about me, since it’s my first post. ;)
The task at hand is to get the users postcode on application launch. I’ve been using an AsyncTask for the reverse geocoding and It generally seems to work. But only when I call the ReverseGeocodingTask on a button click, and give it a few seconds before I do so. If I press it immediately it sometimes works and sometimes doesn’t, so obviously when I call it in the onCreate method the app crashes aswell. It also crashes when I turn the internet off on the phone. I reckoned the network provider location should be sufficient and there is no need for the GPS accuracy and the additional permissions.
If the INet is turned off by the user, it should just show a message that the postcode can’t be found and give the user the option to input it manually.
I figured that the currentLocation to pass to the geocoding has not been found yet and is throwing a NullPointerException, so I tried to prevent that by checking it before the call. But that didn’t really help and is no solution for the final version anyways.
Since its always best to show the code so u guys know what’s going on, here goes:
Code: Select all
package com.adix.DroidTest;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.*;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicReference;

import static java.util.Locale.getDefault;

public class MyActivity extends Activity implements View.OnClickListener {

    Button getPostCode, confirm;
    TextView tvPostcode;
    LocationManager locationManager;
    Location currentLocation;
    double currentLatitude;
    double currentLongitude;
    private Handler mHandler;
    private static final int UPDATE_ADDRESS = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        init();

        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

        AtomicReference<LocationListener> locationListener = new AtomicReference<LocationListener>(new LocationListener() {
            public void onLocationChanged(Location location) {
                updateLocation(location);
            }

            private void updateLocation(Location location) {
                currentLocation = location;
                currentLatitude = currentLocation.getLatitude();
                currentLongitude = currentLocation.getLongitude();
            }

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

            public void onProviderEnabled(String provider) {
            }

            public void onProviderDisabled(String provider) {
            }
        });
       locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener.get());
        //getAddress();

        mHandler = new Handler() {
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case UPDATE_ADDRESS:
                        tvPostcode.setText((String) msg.obj);
                        break;
                }
            }
        };
    }

    private void init() {
        getPostCode = (Button)findViewById(R.id.bGetPostCode);
        confirm = (Button)findViewById(R.id.bConfirm);
        tvPostcode = (TextView)findViewById(R.id.tvPostcode);

        getPostCode.setOnClickListener(this);
        confirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.bGetPostCode:

                currentLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                if(currentLocation != null) {
                    Log.d("TRACE",currentLocation.toString());
                    Toast.makeText(this, "Suche Postleitzahl", Toast.LENGTH_LONG).show();
                    (new ReverseGeocodingTask(this)).execute(new Location[]{currentLocation});
                }

                break;
            case R.id.bConfirm:
                Intent i = new Intent(MyActivity.this, MainMenu.class);
                startActivity(i);
                finish();

        }

    }

    private class ReverseGeocodingTask extends AsyncTask<Location, Void, Void> {

        Context mContext;

        public ReverseGeocodingTask(Context context) {
            super();
            mContext = context;
        }
        @Override
        protected Void doInBackground(Location... locations) {

            try{

                Geocoder gcd = new Geocoder(mContext, Locale.getDefault());
                List<Address> addresses = gcd.getFromLocation(currentLatitude, currentLongitude,100);
                Address address =  addresses.get(0);
                StringBuilder result = new StringBuilder();
                result.append(address.getPostalCode());
               // tvPostcode.setText(result.toString());
                Message.obtain(mHandler, UPDATE_ADDRESS, result.toString()).sendToTarget();
            }
            catch(IOException ex){
                tvPostcode.setText(ex.getMessage().toString());
                Message.obtain(mHandler, UPDATE_ADDRESS, ex.getMessage().toString()).sendToTarget();
            }
            return null;
        }
    }
}
M4tchB0X3r
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Feb 18, 2013 12:06 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 24 guests