今天调试了Android上调用GPS的功能,在网上找了很多文章,方法是:
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE );//初始化LocationManager
if(!locationManager .isProviderEnabled(android.location.LocationManager. GPS_PROVIDER))//检测是否打开了GPS功能,其他的类似,例如NETWORK_PROVIDER
location = locationManager.getLastKnownLocation( android.location.LocationManager. GPS_PROVIDER );//通过这句来获得Location
最重要的是设置LocationListener,这样就可以随时监听最新的位置更新和定位功能的开启状态了:
locationManager .requestLocationUpdates(android.location.LocationManager.GPS_PROVIDER,60000,5,locationListener ,Looper.myLooper());//第二个参数和第三个参数设置了更新的频率
locationListener变量的声明:
privatefinalLocationListener locationListener =newLocationListener() {//根据需求重写下面四个方法
publicvoidonLocationChanged(Location loc) {
}
// provider 被用户关闭后调用
publicvoidonProviderDisable