您的位置:

Android GPS 开发详解

一、获取GPS信息

Android提供了LocationManager类来获取GPS信息,首先需要检查是否存在GPS模块,并且需要为程序添加获取GPS信息的权限。

1、检查是否存在GPS模块

boolean hasGps = context.getPackageManager()
                          .hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS);
if (!hasGps) {
    // 没有GPS模块
}

2、添加权限

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

二、位置更新

使用LocationManager的requestLocationUpdates方法获取GPS信息,并且设置更新时间和距离。

1、启动位置更新

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE, locationListener);

2、停止位置更新

locationManager.removeUpdates(locationListener);

三、位置监听

添加LocationListener监听GPS信息的变化,并在回调方法中处理相关逻辑。

1、添加监听器

LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        // 处理定位变化逻辑
    }
 
    public void onStatusChanged(String provider, int status, Bundle extras) {}
 
    public void onProviderEnabled(String provider) {}
 
    public void onProviderDisabled(String provider) {}
};

2、获取位置信息

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

四、完整代码示例

public class MainActivity extends AppCompatActivity implements LocationListener {
 
    private static final int MIN_TIME = 1000;
    private static final int MIN_DISTANCE = 10;
    private LocationManager locationManager;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    }
 
    @Override
    protected void onResume() {
        super.onResume();
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE, this);
        } else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    1);
        }
    }
 
    @Override
    protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this);
    }
 
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE, this);
            }
        }
    }
 
    @Override
    public void onLocationChanged(Location location) {
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        // 处理定位变化逻辑
    }
 
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
 
    @Override
    public void onProviderEnabled(String provider) {
    }
 
    @Override
    public void onProviderDisabled(String provider) {
    }
}