简介

华为定位服务采用GPS、WiFi、基站等多途径的混合定位模式进行定位,赋予应用开发者快速、精准地获取用户位置信息的能力,构建全球定位服务能力,助力开发者发展全球业务。

您将建立什么

在这个Codelab中,你将创建一个获取设备位置信息的APP。

您将会学到什么

硬件要求

软件要求

集成HUAWEI HMS能力,需要完成以下准备工作

具体操作,请按照《HMS Core 3.0集成准备》中详细说明来完成。

本次Codelab中,您将调用Location Kit定位服务能力创建一款android 应用程序(Codelab app)。本应用程序的基本架构如图所示:Codelab app将集成LocationSDK,然后通过LocationSDK去调用HMSAPK的定位服务能力。

在本次Codelab中,您需要在Android studio 上边新建您的工程。

主要配置
打开AndroidStudio项目级build.gradle文件。

添加编译依赖,打开应用级build.gradle文件

开发者编译APK前需要配置不要混淆HMS SDK,避免功能异常。

  1. 打开Android工程的混淆配置文件proguard-rules.pro。
  2. 加入排除HMS SDK的混淆配置。
    -ignorewarning -keepattributes *Annotation* -keepattributes Exceptions -keepattributes InnerClasses -keepattributes Signature -keepattributes SourceFile,LineNumberTable -keep class com.hianalytics.android.**{*;} -keep class com.huawei.updatesdk.**{*;} -keep class com.huawei.hms.**{*;}
  3. 如果开发者使用了AndResGuard,需要在混淆配置文件中加入AndResGuard白名单。
    "R.string.hms*", "R.string.connect_server_fail_prompt_toast", "R.string.getting_message_fail_prompt_toast", "R.string.no_available_network_prompt_toast", "R.string.third_app_*", "R.string.upsdk_*", "R.layout.hms*", "R.layout.upsdk_*", "R.drawable.upsdk*", "R.color.upsdk*", "R.dimen.upsdk*", "R.style.upsdk*"

在Android Q版本中,如果您需要应用程序在后台执行时也具备持续定位能力,需要在Manifest文件中添加ACCESS_BACKGROUND_LOCATION权限:

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

RequestLocationUpdatesWithCallbackActivity.Java

  1. 创建位置信息提供者客户端和设备设置客户端。
    //create a fusedLocationProviderClient fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this); //create a settingsClient settingsClient = LocationServices.getSettingsClient(this);
  2. 创建位置信息请求体。
    mLocationRequest = new LocationRequest(); // set the interval for location updates, in milliseconds. mLocationRequest.setInterval(10000); // set the priority of the request mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
  3. 创建结果回调类 。
    mLocationCallback = new LocationCallback() { @Override public void onLocationResult(LocationResult locationResult) { if (locationResult != null) { List<Location> locations = locationResult.getLocations(); if (!locations.isEmpty()) { for (Location location : locations) { Log.i(TAG, "onLocationResult location[Longitude,Latitude,Accuracy]:" + location.getLongitude() + "," + location.getLatitude() + "," + location.getAccuracy()); } } } } @Override public void onLocationAvailability(LocationAvailability locationAvailability) { if (locationAvailability != null) { boolean flag = locationAvailability.isLocationAvailable(); Log.i(TAG, "onLocationAvailability isLocationAvailable:" + flag); } } };
  4. 请求位置更新。
    /** * function:Requests location updates with a callback on the specified Looper thread. * first:use SettingsClient object to call checkLocationSettings(LocationSettingsRequest locationSettingsRequest) method to check device settings. * second: use FusedLocationProviderClient object to call requestLocationUpdates (LocationRequest request, LocationCallback callback, Looper looper) method. */ private void requestLocationUpdatesWithCallback() { try { LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder(); builder.addLocationRequest(mLocationRequest); LocationSettingsRequest locationSettingsRequest = builder.build(); // check devices settings before request location updates. settingsClient.checkLocationSettings(locationSettingsRequest) .addOnSuccessListener(new OnSuccessListener<LocationSettingsResponse>() { @Override public void onSuccess(LocationSettingsResponse locationSettingsResponse) { Log.i(TAG, "check location settings success"); //request location updates fusedLocationProviderClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.getMainLooper()).addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void aVoid) { Log.i(TAG, "requestLocationUpdatesWithCallback onSuccess"); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(Exception e) { Log.e(TAG, "requestLocationUpdatesWithCallback onFailure:" + e.getMessage()); } }); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(Exception e) { Log.e(TAG, "checkLocationSetting onFailure:" + e.getMessage()); int statusCode = ((ApiException) e).getStatusCode(); switch (statusCode) { case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: try { ResolvableApiException rae = (ResolvableApiException) e; rae.startResolutionForResult(RequestLocationUpdatesWithCallbackActivity.this, 0); } catch (IntentSender.SendIntentException sie) { Log.e(TAG, "PendingIntent unable to execute request."); } break; } } }); } catch (Exception e) { Log.e(TAG, "requestLocationUpdatesWithCallback exception:" + e.getMessage()); } }
  5. 移除位置信息更新请求。
    private void removeLocationUpdatesWithCallback() { try { fusedLocationProviderClient.removeLocationUpdates(mLocationCallback) .addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void aVoid) { Log.i(TAG, "removeLocationUpdatesWithCallback onSuccess"); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(Exception e) { Log.e(TAG, "removeLocationUpdatesWithCallback onFailure:" + e.getMessage()); } }); } catch (Exception e) { Log.e(TAG, "removeLocationUpdatesWithCallback exception:" + e.getMessage()); } }

在本次codelab 中,你可以点击请求位置更新按钮,并将位置信息打印到当前界面。

干得好,你已经成功完成了Codelab并学到了:

如需了解更多与华为Location Kit相关的信息,请关注我们的官方网站。开发过程中如果遇见问题,请参考我们的常见问题解答

您可以阅读下面链接,了解更多相关的信息。

相关文档

本Codelab中的demo源码下载地址如下:

源码下载

已复制代码