0

I was trying to implement a basic location based program in android and got stuck at first page itself.

This was done using a typical example using the LocationManager and LocationListener interface here is the code

    public class MainActivity extends ActionBarActivity 
{
MyLocationListner networkListner,gpsListner;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button start = (Button) findViewById(R.id.btn_start);
    start.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        startListening();

        }
    });

    Button stop = (Button) findViewById(R.id.btn_stop);
    stop.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v)
        {
            stopListning();

        }
    });

}   

public void startListening()
{
    try {

        LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
        Log.i("LocationApp","Started Listning");
        networkListner = new MyLocationListner(this);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, networkListner);
    } catch (Exception e) {
        Log.i("myException",""+e);
    }
}

public void stopListning()
{
    LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (networkListner == null)
    {
        lm.removeUpdates(networkListner);
        networkListner = null;
    }
}

public void myToast (String message)
{
    Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}

}

Class which implements LocationListner

    public class MyLocationListner implements LocationListener 
{
 Context ctx;



public MyLocationListner(Context context) 
{
    this.ctx = context;
}

private static String LOG_TAG;

@Override
public void onLocationChanged(Location location) 
{
    Log.i(LOG_TAG,"lattitude");
    MyToast("OnLocation changeD");
    String provider = location.getProvider();
    double lat = location.getLatitude();
    double lon = location.getLongitude();
    float accuracy = location.getAccuracy();
    long time = location.getTime();

    MyToast(lat+"X"+lon+"Accuracy :"+accuracy);
    Log.i(LOG_TAG,"lattitude :"+lat+"\nLongitude :"+lon+"\nAccuracy :"+accuracy);

}

@Override
public void onProviderDisabled(String arg0) {
    Log.i(LOG_TAG,"Provider Disabled");

}

@Override
public void onProviderEnabled(String arg0) {
    Log.i(LOG_TAG,"Provider Enabled");
}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) 
{

}

public void MyToast(String message)
{
    Toast.makeText(ctx, message, Toast.LENGTH_SHORT).show();
}
}

and not a single method inside the above class is invoking , I have gone through the tutuorial two three times but still no hope!! I know I'm doing something wrong but cant find it.

I've tried running it on a real device also. Advices and answers needed Tahnks

P-RAD
  • 1,293
  • 2
  • 15
  • 36
  • Have you added the permissions? – Skynet Jul 07 '14 at 13:13
  • Look at my answer here it might help you. http://stackoverflow.com/questions/24598609/gps-coordinates-using-location-manager-get-printed-as-null/24600264#24600264 – SeahawksRdaBest Jul 07 '14 at 13:36
  • @ SeahawksRdaBest was a great post and I'll definitely use asycTask.,but for the moment all I want to know is why these methods never get called (in emulator and real device) , this is one of simplest things and yet cant figured out what's wrong!!:( – P-RAD Jul 07 '14 at 13:46

0 Answers0