Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize simulated Positioning in Android

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to achieve simulation positioning in Android, I hope you will gain something after reading this article, let's discuss it together!

Positioning simulation and route playback are often needed in navigation test scenarios, and the simulation status is recorded through the LocationManager.setTestProviderLocation () method. If the application to be tested does not support TestProviderLocation simulation position input, you can consider starting from the HAL layer, the default GPS implementation of the hook system.

1. Enable configuration of Android simulation permissions

In the versions below Android6.0, you need to check the switch for analog positioning in the settings, and change it to an application that selects analog positioning above 6.0.The corresponding configuration method is also different. the same is that the following two permissions need to be configured in AndroidManifest.xml:

1) turn on the analog positioning switch Settings.Secure.putInt (getContentResolver (), Settings.Secure.ALLOW_MOCK_LOCATION, 1) below Android 6.0)

To enable analog location in this way, you need to configure the following system permissions in AndroidManifest.xml, and the application also needs to be signed by the system, which cannot be implemented in this way for non-system applications.

Android:sharedUserId= "android.uid.system"

When it comes to this operation, you can bypass the system permission configuration by configuring it with the adb shell command:

Adb shell settings put secure mock_location 12) the application of code configuration above Android 6. 0 to select simulated location

For Android versions above 6. 0, you need to set the mock_location permission for the specified package name to allow.

Try {String mockLocationPkgName = getPackageName (); PackageManager mPackageManager = getPackageManager (); final ApplicationInfo ai = mPackageManager.getApplicationInfo (mockLocationPkgName, PackageManager.MATCH_DISABLED_COMPONENTS); AppOpsManager mAppsOpsManager = (AppOpsManager) getSystemService (Context.APP_OPS_SERVICE); mAppsOpsManager.setMode (AppOpsManager.OPSTR_MOCK_LOCATION, ai.uid, mockLocationPkgName, AppOpsManager.MODE_ALLOWED); catch (PackageManager.NameNotFoundException e) {/ * ignore * /}

At the same time, the following permissions need to be configured in AndroidManifest.xml. Unfortunately, this method also needs to be compiled with the system signature and source code, and can only be used by applications at the system level.

In order to display the application of simulated positioning in the simulated positioning options of the setting interface, you need to configure ACCESS_MOCK_LOCATION permissions.

The above configuration can also be achieved through the adb shell command, and the parameters are replaced with the package name applied by yourself.

Adb shell appops set android:mock_location allow

Turn off the permission to simulate location, and use the following command:

Adb shell appops set android:mock_location deny

To query which applications have simulated location permissions enabled, use the following command:

Adb shell appops query-op android:mock_location allow

After execution, the applied package name list parameters are output.

Second, the realization of Android simulation positioning.

1) Analog positioning switch check

First of all, the code first determines whether the analog location permission is enabled, and those above 6.0 can only be judged by adding location monitoring whether there is an exception.

Boolean mockPermission = false;if (Build.VERSION.SDK_INT = 17) {loc.setElapsedRealtimeNanos (SystemClock.elapsedRealtimeNanos ());} locationManager.setTestProviderLocation (LocationManager.GPS_PROVIDER,loc)

Let's see why you need to set the elapsedRealtimeNanos and time parameters. There is no setElapsedRealtimeNanos method for Location (Android 4.1.1) below SDK version 17. Starting from SDK version 17, Location (Android 4.2) adds this method. When calling setTestProviderLocation to set location information, Android SDK version 17 or above will check whether the positioning parameters are complete, and versions below 17 will automatically supplement them, and versions starting from 17 will directly throw an exception.

Public void setTestProviderLocation (String provider, Location loc) {if (! loc.isComplete ()) {IllegalArgumentException e = new IllegalArgumentException ("Incomplete location object, missing timestamp or accuracy?" + loc); if (mContext.getApplicationInfo () .targetSdkVersion

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report