本人初学Android,最近尝试做一个GPS练习,通过上网查找资料自己学习开发,但是测试过程中出现问题 provider的值一直为空,上网查找资料有说没加权限的 有说需要加“locationManager.setTestProviderEnabled("gps", true);”这行代码的,我都试验了一遍但是仍然报错。困扰了很长时间,最后终于发现问题所在跟大家分享一下希望对大家有帮助。

我的问题是出在权限上,我最开始找到的资料上权限是这样加的

<uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
 <uses-permission android:name="android.permission.ACCESS_FIND_LOCATION" />注意这行代码

后来发现应该是

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />注意是FINE而不是FIND

修改之后 问题解决,如获重释一般的感觉。下面把我的测试通过的代码分享一下 希望对学习GPS的朋友有所帮助。

我的GPSActivitys

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class GPSActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        LocationManager locationManager;
        String serviceName = Context.LOCATION_SERVICE;
        locationManager = (LocationManager)getSystemService(serviceName);
        //String provider = LocationManager.GPS_PROVIDER;
       
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        String provider = locationManager.getBestProvider(criteria, true);
       
        Location location = locationManager.getLastKnownLocation(provider);
        //updateWithNewLocation(location);
        locationManager.requestLocationUpdates(provider, 2000, 10,
                        locationListener);
        updateWithNewLocation(location);
    }
   
    private final LocationListener locationListener = new LocationListener() {
  public void onLocationChanged(Location location) {
   updateWithNewLocation(location);
  }

  public void onProviderDisabled(String provider) {
   updateWithNewLocation(null);
  }

  public void onProviderEnabled(String provider) {
  }

  public void onStatusChanged(String provider, int status, Bundle extras) {
  }
 };
 
 private void updateWithNewLocation(Location location) {
  String latLongString;
  TextView myLocationText;
  myLocationText = (TextView) findViewById(R.id.myLocationText);//这个自己设计一个TextViews
  if (location != null) {
   double lat = location.getLatitude();
   double lng = location.getLongitude();
   latLongString = "纬度:" + lat + "\n经度:" + lng;
  } else {
   latLongString = "无法获取地理信息";
  }
  myLocationText.setText("您当前的位置是:\n" + latLongString);
 }
}

 

AndroidManifest.xml代码如下

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.gzsk.gps"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".GPSActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

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