0

I'm making an app that sends the present location through sms. But the location listener method is not called. It says:

method LocationListener is never called

Here's my code :

public class MainActivity2 extends Activity {

    LocationManager lm;
    LocationListener locationListener;

    private void LocationListener(Context context) {
        lm = (LocationManager)
                context.getSystemService(Context.LOCATION_SERVICE);

        locationListener = new MyLocationListener();
        lm.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER,
                60000,
                1000,
                locationListener);

    }

    private class MyLocationListener implements LocationListener {
        @Override
        public void onLocationChanged(Location loc) {
            if (loc != null) {
                String Uri = "http://maps.google.com/maps?saddr=" + loc.getLatitude() + "," + loc.getLongitude();

                SmsManager smsManager = SmsManager.getDefault();
                StringBuffer smsBody = new StringBuffer();
                smsBody.append(Uri);
                smsManager.sendTextMessage("+91847690****", null, smsBody.toString(), null, null);

                lm.removeUpdates(locationListener);
            }
        }

        public void onProviderDisabled(String provider) {
        }

        public void onProviderEnabled(String provider) {
        }

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
        return true;
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();


        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
OhBeWise
  • 5,350
  • 3
  • 32
  • 60
  • I set up the LocationListener in the onCreate() method. I don't see where your code ever references your LocationListener class. http://stackoverflow.com/questions/30824549/why-does-map-marker-lurch-around-the-map – user1091524 Jul 07 '15 at 18:40

1 Answers1

0

It looks like the confusion is that you have created a method in your MainActivity2 class that is called LocationListener(). This is not good, since you shouldn't have a method with the same name as the LocationListener class.

Just rename the method, and since this method is part of your Activity, you don't need to pass in a Context:

private void setUpLocationListener() {
    lm = (LocationManager)
            this.getSystemService(Context.LOCATION_SERVICE);

    locationListener = new MyLocationListener();
    lm.requestLocationUpdates(
            LocationManager.NETWORK_PROVIDER,
            60000,
            1000,
            locationListener);

}

Then, just call the method from onCreate():

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    setUpLocationListener();
}
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137