实现计时器以获取gps位置

时间:2014-11-22 05:31:11

标签: android gps countdowntimer

我想从gps获取位置。因为gps需要一些时间来获取位置所以我需要添加一个 倒计时器。如果时间到期想要从网络获取位置。 我是android和java的初学者,我按照以下方式实现,但我没有成功。 请清除我的错误或提供任何示例代码。提前谢谢。

if (isGPSEnabled) { 
        CountDownTimer countDownTimer = new CountDownTimer(1000, 3000); 
        {
            public void onTick(long millisUntilFinished) {

                 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,time,distance, this);
                 Log.d("GPS Enabled", "GPS Enabled");
                 if (locationManager != null) {
                    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                 if (location != null) {
                       Latitude =  location.getLatitude();
                       Longitude =  location.getLongitude();

                         String Text =  " From GPS: latitude = " + Latitude +" longitude = " + Longitude + " Device Id: " + device_id;
                         SmsManager smsManager = SmsManager.getDefault();
                         smsManager.sendTextMessage(phoneNo, null, Text, null, null);
                         Log.i("Send SMS", "");
                         this.abortBroadcast();
                   }
                }
            }

       public void onFinish() {
            if(locationManager == null)
                {
                   locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,time,distance, this);
               Log.d("Network", "Network");
               if (locationManager != null) {
               location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
               if (location != null) {
               Latitude  =    location.getLatitude();
               Longitude =    location.getLongitude();
               String Text =  " From Network: latitude = " + Latitude +" longitude = "  + Longitude+ " Device Id: " + device_id;
                   SmsManager smsManager = SmsManager.getDefault();
               try{
                  smsManager.sendTextMessage(phoneNo, null, Text, null, null);
                  Log.i("Send SMS", "");
          this.abortBroadcast();           
          } catch (Exception e) {
           e.printStackTrace(); }
   }

2 个答案:

答案 0 :(得分:1)

您的方法是错误的而不是使用计时器在gps和移动网络之间切换您应该使用locationlistener接口,它将根据您设置的时间监听位置更新。这是我从中获取帮助的链接实施了相同的。

http://developer.android.com/training/location/receive-location-updates.html

答案 1 :(得分:1)

public void turnGPSOn() {

        String provider = Settings.Secure.getString(getContentResolver(),
                Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        if (!provider.contains("gps")) {
            final Intent poke = new Intent();
            poke.setClassName("com.android.settings",
                    "com.android.settings.widget.SettingsAppWidgetProvider");
            poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
            poke.setData(Uri.parse("3"));
            sendBroadcast(poke);
        }
    }


private class MyTimerTask extends TimerTask {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // code to get and send location information
                    locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                    if (!locManager
                            .isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                        turnGPSOn();
                    }

                    try {
                        locManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER, 1000, 10,
                                locationListener);

                    } catch (Exception ex) {
                        turnGPSOff();
                    }

                }

            });
        }
    }

    private void updateWithNewLocation(Location location) {
        String latLongString = "";
        try {
            if (location != null) {

                Log.e("test", "gps is on send");
                latitude = Double.toString(location.getLatitude());
                longitude = Double.toString(location.getLongitude());

                Log.e("test", "location send");

                locManager.removeUpdates(locationListener);


                latLongString = "Lat:" + latitude + "\nLong:" + longitude;
                Log.w("CurrentLocLatLong", latLongString);
            } else {
                latLongString = "No location found";
            }
        } catch (Exception e) {
        }

    }

    private final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            updateWithNewLocation(location);
        }

        public void onProviderDisabled(String provider) {
            updateWithNewLocation(null);
        }

        public void onProviderEnabled(String provider) {
        }

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

Calling the timer :

long gpsTimeInterval=2000;
void startTimer()
{

myTimer = new Timer();
                                myTimerTask = new MyTimerTask();
                                myTimer.scheduleAtFixedRate(myTimerTask, 0,
                                        gpsTimeInterval);
}