2012-05-03 167 views
1

我想檢查設備是否從衛星獲取數據。 如果我得到我目前的位置,然後我做一些事情。否則,如果我還沒有任何座標,我想爲我留言。 我怎麼知道,我是否已經獲得座標?檢查GPS輔助功能

GeoPoint h = null; 
double latitude = 10, longitude = 10; 
public void onClick(View v) 
{ 
    switch (v.getId()) 
    { 
     case R.id.get_GPS: 
      final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
      if(isInternetOn() && manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) 
      { 
//I want here to check, if I get latitude and longitude values, which differ from 10 x 10, I go to my map, otherwise I say, that there's no information received from satellite. 
/* 
       h = new GeoPoint((int) (latitude * 1000000), (int) (longitude * 1000000)); 

       if(h.getLatitudeE6()==h.getLongitudeE6() && h.getLongitudeE6()==10000000) 
       { 
        Toast.makeText(this, "No information received from satellite yet.", Toast.LENGTH_LONG).show(); 
        break; 
       } 
       */ 
       Intent i = new Intent (this,getGPS.class); 
       startActivity(i); 
      } 
      else 
      { 
       if(!isInternetOn() && !manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) 
       { 
        buildAlertMessageNoGpsNoInternet(); 
        break; 
       } 

       if(!isInternetOn()) 
       { 
        buildAlertMessageNoInternet(); 
        break; 
       } 
       if(!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) 
       { 
        buildAlertMessageNoGps(); 
        break; 
       } 
      } 

     break; 
     case R.id.about_button: 
     Intent j = new Intent(this, About.class); 
     startActivity(j); 
     break; 
     case R.id.exit_button: 
     this.moveTaskToBack(true); 
     break; 
    } 
} 

山姆,我得到空指針異常的 「如果」:

if(manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) 
      { 

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

       /*Here ->*/if(location.getTime() > ((Calendar.getInstance().getTimeInMillis()) - 10000)) 
       { 
        Intent i = new Intent (this, getGPS.class); 
        startActivity(i); 
       } 

       else 
       { 
        // Wait for new location 
        LocationListener locationListener = new myLocationListener(); 
        manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationListener); 
       } 
} 

logcat的

11月5日至5日:42:49.444:E/AndroidRuntime(300):java.lang中.NullPointerException

+1

請發佈您嘗試過的任何logcat錯誤,否則[位置和地圖]上的開發人員指南(http://developer.android.com/guide/topics/location/index.html )是一個開始的好地方。 – Sam

+0

@Sam,沒有錯誤得到。只要得到循環烤麪包。 – azizbekian

+0

重新編輯該問題。 – azizbekian

回答

1

我會嘗試沿着此線的東西:

public class Example extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

     if(location.getTime() > Calendar.getInstance().getTimeInMillis() - 2 * 60 * 1000) { 
      // Do something with the recent location fix 
     } 
     else { 
      // Wait for new location 
      LocationListener locationListener = new myLocationListener(); 
      manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationListener); 
     } 
    } 

    private class myLocationListener implements LocationListener { 
     public void onLocationChanged(Location location) { 
      if (location != null) { 
       Log.v("Location Changed", location.getLatitude() + " " + location.getLongitude()); 
       // Do something with current location 
      } 
     } 

     // Must implement missing functions! 
    } 
} 

加成

如果位置是空在這裏,可能意味着有出於某種原因沒有最後已知的位置。簡單修復:

if(location != null && location.getTime() > ((Calendar.getInstance().getTimeInMillis()) - 10000))