In order to protect user privacy, most applications will only obtain the user's location when they are running in the foreground. When the application is running in the background, the location function will be disabled. As a result, the APP cannot record the GPS track normally in the background or when the screen is locked. This has a great impact on applications that need to record the user's track in real time, such as taxi-hailing, shared travel, and running, and even affects the experience of using the core functions of the application. So for the developers of these applications, how can the application maintain continuous positioning for a long time when the application is running in the background after the user actively authorizes the location information?
The HMS Core location service provides the ability to continuously locate in the background, and can permanently record location information under the condition of obtaining the user's active authorization, which is suitable for track recording scenarios.
1. Fusion Positioning-Background Positioning Implementation Method
- The app runs on a non-Huawei phone
- After using LocationCallback to enable positioning, when the application switches to the background, the positioning will stop soon.
- In order to make the positioning capability still valid after the application switches to the background, you can use the enableBackgroundLocation method to create a foreground service to increase the frequency of the application's location update in the background.
- Background positioning itself does not have positioning capabilities, and background positioning needs to be used together with the positioning enabled by LocationCallback. The data obtained from the location needs to be obtained in the onLocationResult(LocationResult locationResult) method in the LocationCallback object.
2. Matters needing attention:
- Supported devices are non-Huawei phones
- App needs location permission and must be "Always Allow"
- The application cannot be frozen by the power control application such as the power saving wizard in the device. Take the vivo mobile phone as an example: open the i housekeeper - background power consumption management - find the application - change the intelligent power control to allow high power consumption in the background.
3. Precautions when testing Demo:
- It is best not to charge the device during the test, and the application may not be charged in the charged state.
- You can judge whether the device is currently positioning by whether there is a positioning chart in the status bar. Take the vivo mobile phone as an example: when the positioning of the vivo mobile phone is turned on, a positioning icon will be displayed in the status bar. If the background positioning is not enabled, the application and the background positioning icon will disappear. After the background positioning capability is turned on, the application-to-background positioning icon still exists.
Fourth, realize the integration steps of background positioning function
- Add background location service in AndroidManifest.xml
<service
android:name="com.huawei.location.service.BackGroundService"
android:foregroundServiceType="location" />
- (Optional) In Android 9 and above, to ensure the normal use of background positioning permissions, you need to configure the FOREGROUND_SERVICE permission in the "AndroidManifest.xml" file:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
- Create Notification object
public class NotificationUtil {
public static final int NOTIFICATION_ID = 1;
public static Notification getNotification(Context context) {
Notification.Builder builder;
Notification notification;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
String channelId = context.getPackageName();
NotificationChannel notificationChannel =
new NotificationChannel(channelId, "LOCATION", NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(notificationChannel);
builder = new Notification.Builder(context, channelId);
} else {
builder = new Notification.Builder(context);
}
builder.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Location SDK")
.setContentText("Running in the background ")
.setWhen(System.currentTimeMillis());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notification = builder.build();
} else {
notification = builder.getNotification();
}
return notification;
}
}
- Initialize the FusedLocationProviderClient object
FusedLocationProviderClient mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
- Enable background positioning
private void enableBackgroundLocation() {
mFusedLocationProviderClient
.enableBackgroundLocation(NotificationUtil.NOTIFICATION_ID, NotificationUtil.getNotification(this))
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
LocationLog.i(TAG, "enableBackgroundLocation onSuccess");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
LocationLog.e(TAG, "enableBackgroundLocation onFailure:" + e.getMessage());
}
});
}
Learn more details>>
Visit the official website of Huawei Developer Alliance
Get development guidance documents
Huawei Mobile Services Open Source Warehouse Address: GitHub , Gitee
Follow us to know the latest technical information of HMS Core for the first time~
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。