سؤال

Let me give you a brief background.

I need to check the users location either after some time or after some distance. So I am using LocationListner class to track the location. To test it on Emulator, I have created on .gpx file to set the direction. and I am getting the location on emulator currently on button click. But how will I be able to get the updated location based on distance or time. As stated by develper.adnroid

You can control the frequency at which your listener receives updates with the second 
and third parameter—the second is the minimum time interval between notifications and
the third is the minimum change in distance between notifications—setting both to
zero requests location notifications as frequently as possible.

But I am not getting the expected result. Do any body know What's wrong I am doing.??

Code Snippets

On button click this piece of code execute

    LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            MyLocationListener locListener = new MyLocationListener(this, locManager);
            boolean isGPSEnabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
            if (isGPSEnabled) 
            {
                Toast.makeText(getApplicationContext(), "Called isGPSEnabled", Toast.LENGTH_SHORT).show();
                locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 120000, 0, locListener);
                } 

FYI: toast inside if is called.

And this is my onLoactionChange. Adding this method only as rest methods are empty.

@Override
    public void onLocationChanged(Location location) 
    {
           if(location != null)
        {
            locManager.removeUpdates(this);

            Geocoder gcd = new Geocoder(context.getApplicationContext(), Locale.getDefault());       
            List<Address>  addresses; 
            try
            {  
                addresses = gcd.getFromLocation(location.getLatitude(), location.getLongitude(), 1);  
                if (addresses.size() > 0)
                {
                    String cityName = addresses.get(0).getAddressLine(0) + "\n";
                    cityName = cityName + addresses.get(0).getAddressLine(1);
                    TextView tvInfo = (TextView) ((Activity) context).findViewById(R.id.tv_info);
                    tvInfo.setVisibility(View.VISIBLE);
                    tvInfo.setText("Curent address is" + cityName + "Latitude :" + addresses.get(0).getLatitude() + "Longitude :" +addresses.get(0).getLongitude());
                }
            } catch (IOException e) 
            {           
                //Toast
                Toast.makeText(context.getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                e.printStackTrace();  
            } 
        }
    }

Manifest have permissions as

   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
     <uses-permission android:name="android.permission.INTERNET" />

Result: Once button is clicked it gives the current location. but remains that only all the time.

Expected result: Either after some time or some distance moved current location should update.

Can any bode help me in achieving this. or point out where am I doing wrong.??

هل كانت مفيدة؟

المحلول

Seems I need to remove locManager.removeUpdates(this);

Removing this line enabled my application to receive update on emulator.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top