4

我想使用 gps 提供程序获取设备的当前位置,但这实际上是在等待 gps 位置发生变化。

所以我有一个简单的应用程序来监听位置变化。这可以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();
        }
    }
}

我确实将许可放在清单中,所以这不是问题。

4

2 回答 2

2

你在里面测试吗?除非您在外面,否则您可能根本无法获得 GPS 锁定,这意味着手机将尽可能多地尝试获得锁定(因为您在 requestLocationUpdates 调用中指定了最短时间和距离为 0 和 0 )。

不幸的是,您只需要等待设备获得 GPS 锁定,如果不是因为信号不足,您将继续等待。缓解这种情况的第一件事是尝试获取 GPS 提供商的最后一个已知位置。如果设备最近有 GPS 锁定并存储了一个位置,这将返回一个位置:

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

除此之外,您应该尝试从网络提供商那里获取位置,因为正如您已经观察到的那样,这可能会比 GPS 更快地返回位置。这是因为位置数据(手机信号塔和 WIFI)来源丰富,只要您有无线信号,就可以在任何地方(包括室内)使用。

于 2012-12-10T16:29:17.613 回答
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);

    } 
于 2014-03-29T00:09:25.273 回答