简单的网络位置测试不起作用?

时间:2013-02-27 14:48:01

标签: android

我只想查看从粗略位置修复返回的位置的形式。使用谷歌代码:

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


     // Acquire a reference to the system Location Manager
        LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        // Define a listener that responds to location updates
        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
              // Called when a new location is found by the network location provider.
              makeUseOfNewLocation(location);
            }


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

            public void onProviderEnabled(String provider) {}

            public void onProviderDisabled(String provider) {}
          };

        // Register the listener with the Location Manager to receive location updates
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

    }


    private void makeUseOfNewLocation(Location location) {

        TextView locationOutput = (TextView)findViewById(R.id.locationOutput);
        locationOutput.setText((CharSequence) location);

    }

这不适用于我的手机,所以我认为这并不容易。我错过了什么?

TIA

启用当前权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

1 个答案:

答案 0 :(得分:1)

您是否在清单中添加了权限?

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

此外,您可以获取null的位置,确保能够处理它。

修改

尝试getLastKnownLocation(LocationManager.GPS_PROVIDER);或添加wifi权限。