2012-10-17 161 views
1

我能夠從網絡提供商獲取位置更新,但是當涉及到GPS時,需要花費大量時間來挑選數據。我想保留一段時間,只有GPS偵聽器才能工作,然後在某個時間之後轉移到網絡提供商。如何解決這個問題?設置超時GPS偵聽

這是我的代碼..

public void gpslocation() 
    { 
     final LocationManager locationManager = (LocationManager) this 
     .getSystemService(Context.LOCATION_SERVICE); 

     LocationListener locationListener = new LocationListener() { 

    public void onLocationChanged(Location location) 

    { 
     updateLocationForGeo(location); 
     //update(location); 
     // Called when a new location is found by the network location provider. 
     makeUseOfNewLocation(location); 

    } 

    private void makeUseOfNewLocation(Location location) { 
     // TODO Auto-generated method stub 

    } 

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

    public void onProviderEnabled(String provider) { 

     System.out.println(provider+ "enabled provider"); 

    } 


    public void onProviderDisabled(String provider) { 
     // TODO Auto-generated method stub 
     System.out.println(provider+ "disabled provider"); 
     networklocation(); 



    } 

     }; 


    String locationProvider = LocationManager.GPS_PROVIDER; 
    locationManager.requestLocationUpdates(locationProvider, 10 * 1000, (float) 10.0,locationListener); 


      } 
    public void networklocation() 
    { 
     final LocationManager locationManager = (LocationManager) this 
     .getSystemService(Context.LOCATION_SERVICE); 

     LocationListener locationListener = new LocationListener() { 

    public void onLocationChanged(Location location) 

    { 
     updateLocationForGeo(location); 
     //update(location); 
     // Called when a new location is found by the network location provider. 
     makeUseOfNewLocation(location); 

    } 

    private void makeUseOfNewLocation(Location location) { 
     // TODO Auto-generated method stub 

    } 

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

    public void onProviderEnabled(String provider) { 

     System.out.println(provider+ "enabled provider"); 

    } 


    public void onProviderDisabled(String provider) { 
     // TODO Auto-generated method stub 
     System.out.println(provider+ "disabled provider"); 
     isGpsProvidersDisabled=true; 




    } 

     }; 



    String timeProvider = LocationManager.NETWORK_PROVIDER; 
    locationManager.requestLocationUpdates(timeProvider, 1 * 1000, (float) 10.0, locationListener); 

      } 
public void updateLocationForGeo(Location location){ 

    System.out.println("location updated"); 
    double dev_lat = location.getLatitude(); 
    double dev_lang = location.getLongitude(); 
    boolean out_of_range=false; 

    for(int i=0; i<arrayLength; i++){ 

     double lattDiff = Math.toRadians(latarr[i]-dev_lat); 

     double longDiff = Math.toRadians(lonarr[i]-dev_lang); 

     double distance=(Math.sin(lattDiff/2)*Math.sin(lattDiff/2))+(Math.sin(longDiff/2)*Math.sin(longDiff/2)*Math.cos(Math.toRadians(latarr[i]))*Math.cos(Math.toRadians(dev_lat))); 

     System.out.println(distance+" distance"); 
     double c= (2 * Math.atan2(Math.sqrt(distance), Math.sqrt(1-distance))); 



      double radius=radarr[i]* 1.60934; 



      double d = 6371 * c; 


     if(d>radius) 
      { 

     out_of_range=true; 
      continue; 

      } 
     else{ 
      System.out.println("enjoy"); 
      out_of_range=false; 
      break; 
      } 

    } 

任何幫助將不勝感激。

回答

0
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
LocationListener mlocListener = new MyLocationListener(); 
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, 
     mlocListener); 

public class MyLocationListener implements LocationListener { 
private Address mAddresses; 
@Override 
public void onLocationChanged(Location loc) { 
    loc.getLatitude(); 
    loc.getLongitude(); 

    Geocoder gcd = new Geocoder(getApplicationContext(), 
      Locale.getDefault()); 
    try { 
     mAddresses = gcd.getFromLocation(loc.getLatitude(), 
       loc.getLongitude(), 1); 

    } catch (IOException e) { 

    } 

    String cityName = (mAddresses != null) ? mAddresses.get(0) 
      .getLocality() : TimeZone.getDefault().getID(); 
    String countryName = (mAddresses != null) ? mAddresses.get(0) 
      .getCountryName() : Locale.getDefault().getDisplayCountry() 
      .toString(); 


    mCurrentSpeed.setText("Longitude"+loc.getLongitude()+" Latitude"+loc.getLatitude()); 
} 

@Override 
public void onProviderDisabled(String provider) { 
    Toast.makeText(getApplicationContext(), "Gps Disabled", 
      Toast.LENGTH_SHORT).show(); 
} 

@Override 
public void onProviderEnabled(String provider) { 
    Toast.makeText(getApplicationContext(), "Gps Enabled", 
      Toast.LENGTH_SHORT).show(); 
} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 
} 
} 
+0

其實IO我能夠讀出時的網絡供應商可用,但僅通過GPS的時候是沒有發生任何幫助如何檢查GPS是工作或沒有將更多helful – sindy90

+0

如果GPS信號強度是好的,你可以每秒接收更新並隨時更新位置信息。您的氣候條件可能是第一個原因,接下來您的設備GPS功能可能非常低。這些是你無法獲得GPS信號的兩個原因 – RajeshVijayakumar

+0

是的,這是正確的,但我想知道,如果我可以保持一個計時器說2分鐘,如果gps不在那段時間移動到網絡provider.If這是可能的,放置計時器,即在哪個功能(提供啓用,onlocatin改變,onstatuschanged等)? – sindy90