2012-12-10 22 views
4

我想使用gps提供程序獲取設備的當前位置,但實際上等待gps位置發生變化。如何獲得當前的GPS位置而無需等待Android上的位置更改?

所以我有一個簡單的應用程序偵聽的位置變化。這適用於NETWORK_PROVIDER立即,但GPS_PROVIDER我等了15分鐘,並沒有發生任何行動。

我使用API​​級8

public class MainActivity extends Activity { 
    TextView tv; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     tv = (TextView) this.findViewById(R.id.textView1); 
     LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     LocationListener ll = new mylocationlistener(); 

     //lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll); // this works fine 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll); 
    } 

    private class mylocationlistener implements LocationListener { 
     public void onLocationChanged(Location location) { 
      Date today = new Date(); 
      Timestamp currentTimeStamp = new Timestamp(today.getTime()); 
      if (location != null) { 
       Log.d("LOCATION CHANGED", location.getLatitude() + ""); 
       Log.d("LOCATION CHANGED", location.getLongitude() + ""); 
       String str = "\n CurrentLocation: " + "\n Latitude: " 
         + location.getLatitude() + "\n Longitude: " 
         + location.getLongitude() + "\n Accuracy: " 
         + location.getAccuracy() + "\n CurrentTimeStamp " 
         + currentTimeStamp + "\n Altitude: " 
         + location.getAltitude() + "\n Bearing" 
         + location.getBearing() + "\n Speed" 
         + location.getSpeed() + "\n "; 
       Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG) 
         .show(); 
       tv.append(str); 
      } 
     } 

     public void onProviderDisabled(String provider) { 
      Toast.makeText(MainActivity.this, "Error onProviderDisabled", 
        Toast.LENGTH_LONG).show(); 
     } 

     public void onProviderEnabled(String provider) { 
      Toast.makeText(MainActivity.this, "onProviderEnabled", 
        Toast.LENGTH_LONG).show(); 
     } 

     public void onStatusChanged(String provider, int status, Bundle extras) { 
      Toast.makeText(MainActivity.this, "onStatusChanged", 
        Toast.LENGTH_LONG).show(); 
     } 
    } 
} 

我沒有把清單中的權限,所以這不是問題。

+0

你確定你已經啓用了您的GPS? – gsingh2011

+0

是的,我很確定... –

+0

這取決於您的設備以及GPS信號強度,因爲較新的手機具有高敏感的GPS系統,但舊手機需要開闊的天空來檢測信號。 –

回答

2

你測試裏面?除非您身處室外,否則您可能根本無法獲得GPS鎖,這將意味着手機會盡可能頻繁地鎖定鎖(因爲您已在requestLocationUpdates調用中爲最小時間和距離指定了0和0) )。

不幸的是,你只需要等待設備獲得GPS鎖定,如果確實如此,由於缺乏信號的不,你只要繼續等待。您可以做的第一件事是減輕這個負擔,即嘗試獲取GPS提供商的最後一個已知位置。這將返回一個位置,如果該設備最近有一個GPS鎖和存儲位置:

Location lastLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

除此之外,你應該嘗試從網絡供應商的位置,因爲這很可能會回到一個位置就像你已經觀察到的那樣,它比GPS更快。這是因爲大量的位置數據源(手機信號塔和WIFI),無論何時都可以使用,包括室內,只要您有無線信號。

+0

是啊,我在裏面,我並沒有意識到裏面被會讓我失去所有的信號?想我就只能使用模擬器... –

+1

這是一個好主意。還有一個出色的應用程序,其他人稱爲「假GPS」,可讓您模擬位置鎖定以進行測試。我強烈建議你嘗試一下,如果你只需要調試一個基於位置的應用程序。鏈接如下:https://play.google.com/store/apps/details?id = com.lexa.fakegps – mattgmg1990

0
try{ 
       LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 


       //Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
       Location location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

       double longitude = location.getLongitude(); 
       double latitude = location.getLatitude(); 
       String stlongitude = Double.toString(longitude); 
       String stlatitude = Double.toString(latitude); 

       TextView celllocation = (TextView) findViewById(R.id.txtCellLocation); 
       celllocation.setText(stlatitude + " , " + stlongitude); 


       //String uri = String.format(Locale.ENGLISH, "geo:%f,%f", latitude, longitude); 
       //Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri)); 
       //startActivity(intent); 
    } 

    catch (Exception a) { 
     LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

     double longitude = location.getLongitude(); 
     double latitude = location.getLatitude(); 
     String stlongitude = Double.toString(longitude); 
     String stlatitude = Double.toString(latitude); 

     TextView celllocation = (TextView) findViewById(R.id.txtCellLocation); 
     celllocation.setText(stlatitude + " , " + stlongitude); 

    }