【实例简介】需要在android手机的 开发者模式中 开启 【允许模拟位置】即可,开启允许模拟位置的方法详见:
https://jingyan.baidu.com/article/870c6fc306f1f4b03ee4be7f.html
【实例截图】
【核心代码】
package com.xd.mockposition;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Build;
import android.os.SystemClock;
import android.provider.Settings;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class MainActivity extends Activity {
private TextView tvSystemMockPositionStatus = null;
private Button btnStartMock = null;
private Button btnStopMock = null;
private TextView tvProvider = null;
private TextView tvTime = null;
private TextView tvLatitude = null;
private TextView tvLongitude = null;
private TextView tvAltitude = null;
private TextView tvBearing = null;
private TextView tvSpeed = null;
private TextView tvAccuracy = null;
/**
* 位置管理器
*/
private LocationManager locationManager = null;
public LocationManager getLocationManager() {
return locationManager;
}
/**
* 模拟位置的提供者
*/
private List mockProviders = null;
public List getMockProviders() {
return mockProviders;
}
/**
* 是否成功addTestProvider,默认为true,软件启动时为防止意外退出导致未重置,重置一次
* Android 6.0系统以下,可以通过Setting.Secure.ALLOW_MOCK_LOCATION获取是否【允许模拟位置】,
* 当【允许模拟位置】开启时,可addTestProvider;
* Android 6.0系统及以上,弃用Setting.Secure.ALLOW_MOCK_LOCATION变量ÿ