Android LBS体系01 使用Location Manager

Using the Location Manager

 

在manifest中开始展览权力设置

  要运用Android地点服务,供给安装ACCESS_COARSE_LOCATION或者ACCESS_FINE_LOCATION权限。

  就算权力没有安装,将会在运行时抛出三个
SecurityException异常。

  尽管只需求依照互联网的平昔,能够只注解ACCESS_COARSE_LOCATION权力;尤其精确的GPS定位要求评释 ACCESS_FINE_LOCATION 权限。需求专注的是后世已经包涵了前者。

  另外,固然使用中利用了依照网络的稳定,还要注明网络权限。

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

 

ACCESS,赢得LocationManager的一个引用

  LocationManager 是Android地方服务应用中二个重视的类。

  和别的系统服务接近,能够由此调用 getSystemService() 方法得到2个引用。

  倘若你的使用希望在前景进度中收取地方更新,平常供给在Activity的onCreate()方法中调用那个办法:

LocationManager locationManager =
        (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

 

选择Location Provider

  今后的Android都得以经过两种平底技术取得地方的立异,这么些底层技术被架空成LocationProvider 类的靶子。

  各类Location
Provider拥有分歧的本性,比如固定时间、精确度、能源消耗等。

  一般的话,高精确度的Location
Provider,比如GPS,须求更长的一定时间,比较于低精确度的依照互联网的艺术来说。

  获得GPS provider:

LocationProvider provider =
        locationManager.getProvider(LocationManager.GPS_PROVIDER);

 

  能够安装有个别规范,让Android系统选用3个最相仿的非常,从而选出location
provider。

  注意到那几个专业恐怕得不到别的provider,那时候重临贰个null。

  

// Retrieve a list of location providers that have fine accuracy, no monetary cost, etc
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setCostAllowed(false);
...
String providerName = locManager.getBestProvider(criteria, true);

// If no suitable provider is found, null is returned.
if (providerName != null) 
{
   ...
}

 

肯定Location Provider是或不是使能

  一些location
provider能够在设置(Settings)中关闭,比如GPS。实践中最棒先证实一下对象location
provider是或不是使能,能够通过调用isProviderEnabled() 方法完结。

  假若location
provider是disabled状态,你能够提供给用户2个火候去在装置中打开它,通过运行1个action为ACTION_LOCATION_SOURCE_SETTINGS 的Intent来实现。

 

@Override
protected void onStart() 
{
    super.onStart();

    // This verification should be done during onStart() because the system calls
    // this method when the user returns to the activity, which ensures the desired
    // location provider is enabled each time the activity resumes from the stopped state.
    LocationManager locationManager =
            (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    final boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

    if (!gpsEnabled) 
    {
        // Build an alert dialog here that requests that the user enable
        // the location services, then when the user clicks the "OK" button,
        // call enableLocationSettings()
    }
}

private void enableLocationSettings() 
{
    Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivity(settingsIntent);
}

 

参考资料:

  LocationManager

  http://developer.android.com/reference/android/location/LocationManager.html

  LocationProvider

  http://developer.android.com/reference/android/location/LocationProvider.html

  Using the Location Manager:

  http://developer.android.com/training/basics/location/locationmanager.html

 

相关文章