Android 实现获取gps位置的计时器

Android 实现获取gps位置的计时器,android,gps,countdowntimer,Android,Gps,Countdowntimer,我想从全球定位系统得到位置。由于gps获取位置需要一些时间,因此我需要添加一个 倒计时。如果时间到期,您希望从网络获取位置。 我是一个android和java的初学者,我用下面的方法实现了,但没有成功。 请清除我的错误或提供任何示例代码。提前谢谢 if (isGPSEnabled) { CountDownTimer countDownTimer = new CountDownTimer(1000, 3000); { public voi

我想从全球定位系统得到位置。由于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(); }
   }

哇,谢谢。这就是我一直在寻找的。。此代码还将启用gps?@Andrain如果应用程序不在签名级别,则无法切换gps
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);
}