如何每隔1分钟获取当前位置?

发布于 2024-12-07 06:38:53 字数 1565 浏览 0 评论 0原文

我已经实现了演示来显示用户当前的经纬度。 现在我可以看到当前位置的经纬度。 但我想将其设置为每隔 1 分钟显示一次。 代码如下:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener lListener = new mylocationlistener();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, lListener);
    }

    private class mylocationlistener implements LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
            Log.d("LOCATION CHANGED", location.getLatitude() + "");
            Log.d("LOCATION CHANGED", location.getLongitude() + "");
            Toast.makeText(MainActivity.this,
                "My current location:\nLatitude:"+location.getLatitude() + "\nLogitude:" + location.getLongitude(),Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }
    }
}

那么,我应该怎样做才能使其每隔1分钟显示一次当前的经纬度呢? 我想我必须使用线程来实现它。如何使其成为可能?

I have implemented the Demo to show the current lat-long of the User.
Now I am able to see the lat-long of the current Position.
but I want to Set it to to be displayed in every 1 min of interval.
The code is as below:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener lListener = new mylocationlistener();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, lListener);
    }

    private class mylocationlistener implements LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
            Log.d("LOCATION CHANGED", location.getLatitude() + "");
            Log.d("LOCATION CHANGED", location.getLongitude() + "");
            Toast.makeText(MainActivity.this,
                "My current location:\nLatitude:"+location.getLatitude() + "\nLogitude:" + location.getLongitude(),Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }
    }
}

So, What should I have to do to make it to display the current lat-long in every 1 min of interval?
I think I have to use the thread to implement it. How to make it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可爱咩 2024-12-14 06:38:53
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, lListener);

方法中的参数表示 - 提供者、时间、距离和位置侦听器。

因此,要从位置侦听器请求更新,您需要将第二个参数设置为 1000*60,因为时间以毫秒为单位记录。

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, lListener);

The argument in the method signify - provider, time, distance and the location listener.

Therefore to request an update from the Location listener you need to set the second parameter to 1000*60, because the time is recorded in milliseconds.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文