如何获得每半小时android的gps位置?

时间:2013-04-19 09:56:56

标签: android gps location

我必须每半小时打开一次gps并获取位置并将其关闭是否有任何此功能的代码,因为我是Android的新手,请帮助我。

谢谢。

这是我的代码:

public void checkLocation(View v) {
        //initialize location manager
        manager =  (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        //check if GPS is enabled
        //if not, notify user with a toast
        if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Toast.makeText(this, "GPS is disabled.", Toast.LENGTH_SHORT).show();
        } else {
            //get a location provider from location manager
            //empty criteria searches through all providers and returns the best one
            String providerName = manager.getBestProvider(new Criteria(), true);
            Location location = manager.getLastKnownLocation(providerName);

            TextView tv = (TextView)findViewById(R.id.locationResults);
            if (location != null) {
                tv.setText(location.getLatitude() + " latitude, " + location.getLongitude() + " longitude");
            } else {
                tv.setText("Last known location not found. Waiting for updated location...");
            }
            //sign up to be notified of location updates every 15 seconds - for production code this should be at least a minute
            manager.requestLocationUpdates(providerName, 60000*3, 1, this);
        }
    }

4 个答案:

答案 0 :(得分:0)

我会用runnable函数制作它。这是一个很好的example

你应该设置handler.postDelayed(r, 5400000);(如果我计算正确的话)。 值是毫秒。

答案 1 :(得分:0)

我这次没有编码,但我建议您使用系统警报并保持一段时间间隔 半小时。通常在每隔一段时间我们必须做一些工作的地方使用机器人报警器,这样电池也不会受到影响。

答案 2 :(得分:0)

您应该使用requestLocationUpdates方法,该方法可以响应PendindIntent

答案 3 :(得分:-1)

thread1=new Thread(new Runnable() {

        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                try {
                    Thread.sleep(1000*60*30);
                    mHandler.post(new Runnable() {


                        public void run() {
                            // TODO Auto-generated method stub
                            // Write your code here to get the gps location.
                   });
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        }
    });