Android 检查GPS可访问性

Android 检查GPS可访问性,android,gps,satellite,Android,Gps,Satellite,我想检查一下,设备是否从卫星获取数据。 如果我得到了我现在的位置,那么我会做点什么。否则,如果我还没有任何坐标,我想给我留言。 我怎么知道,我是否已经得到了坐标 GeoPoint h = null; double latitude = 10, longitude = 10; public void onClick(View v) { switch (v.getId()) { case R.id.get_GPS: final LocationMan

我想检查一下,设备是否从卫星获取数据。 如果我得到了我现在的位置,那么我会做点什么。否则,如果我还没有任何坐标,我想给我留言。 我怎么知道,我是否已经得到了坐标

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;
    }
}
Sam,“如果”中出现空指针异常:

Logcat


05-05 11:42:49.444:E/AndroidRuntime(300):java.lang.NullPointerException

我会尝试以下方法:

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!
    }
}
添加

如果location在这里为null,则可能意味着无论出于何种原因,都不存在最后一个已知的位置。简单修复:

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

我会尝试这样做:

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!
    }
}
添加

如果location在这里为null,则可能意味着无论出于何种原因,都不存在最后一个已知的位置。简单修复:

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

请发布您尝试过的任何logcat错误,否则《开发人员指南》是一个很好的开始。@Sam,没有错误。只需获取循环toast。请发布您尝试过的任何logcat错误,否则上的开发人员指南是一个很好的开始。@Sam,没有错误。去吃烤面包。