Android中的ACCESS_MOCK_LOCATION权限使用Demo
来源:互联网 发布:大人变小孩软件 编辑:程序博客网 时间:2024/06/05 19:48
The DDMS tool can be used to push out test location during testing. However, it has two serious limitations:
- DDMS sets location for GPS location provider only. If your application uses network provider then you are out of luck.
- DDMS can set location for an emulator device only. You can not do testing using a real device.
If you need to test with real device or using the network location provider, you will need to develop a mock provider within the application. A mock provider can represent any location provider – network or GPS. Writing a mock provider itself is easy. Just be careful about removing the feature before publishing your application.
In this article, we will see how to create a mock location provider.
First, we will develop a class that will encapsulate the details:
public class MockLocationProvider { String providerName; Context ctx; public MockLocationProvider(String name, Context ctx) { this.providerName = name; this.ctx = ctx; LocationManager lm = (LocationManager) ctx.getSystemService( Context.LOCATION_SERVICE); lm.addTestProvider(providerName, false, false, false, false, false, true, true, 0, 5); lm.setTestProviderEnabled(providerName, true); } public void pushLocation(double lat, double lon) { LocationManager lm = (LocationManager) ctx.getSystemService( Context.LOCATION_SERVICE); Location mockLocation = new Location(providerName); mockLocation.setLatitude(lat); mockLocation.setLongitude(lon); mockLocation.setAltitude(0); mockLocation.setTime(System.currentTimeMillis()); lm.setTestProviderLocation(providerName, mockLocation); } public void shutdown() { LocationManager lm = (LocationManager) ctx.getSystemService( Context.LOCATION_SERVICE); lm.removeTestProvider(providerName); }}
A brief description of the MockLocationProvider class may be helpful:
- The constructor takes the name of the location provider that this mock provider will replace. For example, LocationManager.GPS_PROVIDER. The calls to the addTestProvider() and setTestProviderEnabled() tells the LocationManager that the given provider will be replaced by mock data.
- The pushLocation() method supplies mock location data for a given provider.
Any activity or service can easily use the class. Here, we are replacing the network provider with a mock one.
public class MainActivity extends Activity { MockLocationProvider mock; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mock = new MockLocationProvider(LocationManager.NETWORK_PROVIDER, this); //Set test location mock.pushLocation(-12.34, 23.45); LocationManager locMgr = (LocationManager) getSystemService(LOCATION_SERVICE); LocationListener lis = new LocationListener() { public void onLocationChanged(Location location) { //You will get the mock location } //... }; locMgr.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 1000, 1, lis); } protected void onDestroy() { mock.shutdown(); super.onDestroy(); }}
Setting up Security
For mock location to work, certain permissions have to be set.
You will need to request the android.permission.ACCESS_MOCK_LOCATION permission, in addition to any other permission.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/><uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
Finally, you will need to enable mock locations for the device using Settings > Developer options > Allow mock locations.
Disabling Mock Location
It is important that you hide the mock location provider business from the release build. A good way to do that is to enable mock location only if the application is being run in debug mode. In your code, check if debuggable flag is set:
if ((getApplication().getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) { mock = new MockLocationProvider( LocationManager.NETWORK_PROVIDER, this); //Set test location mock.pushLocation(-12.34, 23.45);}
When you test the app using USB debugging or using the emulator, the debug flag is set automatically.
- Android中的ACCESS_MOCK_LOCATION权限使用Demo
- Android中的ACCESS_MOCK_LOCATION权限使用Demo
- Android studio 添加 android.permission.ACCESS_MOCK_LOCATION 权限无法打包
- Android 中的BaseActivity demo
- Android MVP 在项目中的使用 以及demo
- Android RecyclerViewStickyHeaders 使用Demo
- Android RecyclerView使用Demo
- android中的权限
- android中的配置权限
- android中的权限说明
- Android中的权限
- android中的权限大全
- Android中的那些权限
- Android 中的那些权限
- Android 中的权限控制
- android中的权限汇总
- android中的权限
- 王立平--android中的权限
- 学生学籍管理系统_源码下载
- Material design之Compatibility(适配)
- librtmp 时间戳超过 0xffffff时,FMS掉连接
- [Android5.1]开机服务启动顺序
- Android中的Manifest.permission(应用权限)整理
- Android中的ACCESS_MOCK_LOCATION权限使用Demo
- “WebApplication.SqlHelper”的类型初始值设定项引发异常。
- Android截图
- android手机有多个摄像头,打开其中一个
- 获取iOS设备上所有的进程名字,以及进程的ID,
- android发送邮件
- 【爱江山越野跑】ITRA积分认证流程
- iOS 改变TableHeadView的高度,tableView自适应
- 【Google Earth】pro之视频录制