首页 > 代码库 > 在真机上 模拟GPS
在真机上 模拟GPS
必须先开启“允许模拟地点”
方法: 打开设备的,设置 - 应用程序 - 开发 - “允许模拟地点”
1.在 AndroidManifest.xml 里添加相应的 “允许模拟位置的权限”
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
2.创建 一个 “用于模拟的坐标提供者”
LocationManager locationManager = (LocationManager) mContext
.getSystemService(Context.LOCATION_SERVICE);
if (locationManager.getProvider(mMockProviderName) == null) {
locationManager.addTestProvider(mMockProviderName, false, true,
false, false, false, false, false, 0, 5);
locationManager.setTestProviderEnabled(mMockProviderName, true);
}
注意:mMockProviderName 是你随意定的名称,后面在使用(获得位置)时,用这个名称。
3.构建(模拟的)坐标位置
Location loc = new Location(mMockProviderName);
loc.setTime(System.currentTimeMillis());
loc.setLatitude(latitude);
loc.setLongitude(longitude);
locationManager.setTestProviderLocation(mMockProviderName, loc);
Log.i("gps", String.format("once: x=%s y=%s", longitude, latitude));
注意:我们可以多次间歇的调用此方法来 模拟 位置的变化。
4.获得(使用)坐标位置。
loc = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
loc.requestLocationUpdates(NAME_MOCK_GPS_PROVIDER, 1000, 10,
mListener);
这里的NAME_MOCK_GPS_PROVIDER的名称 和前面“第2步”里名称是一致的。
好了。让代码跑起来。
------------------------------------
package pdwy.qyzyMobile.GisModule;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Handler;
import android.util.Log;
class GpsMockManager {
private Context mContext;
Handler mHandler1;
public String mMockProviderName = "spoof";
private int mPostDelayed = 10000;
GpsFakePointProvider mGpsFakePointProvider;
public GpsMockManager(String mockProviderName, Context context,
Handler handler) {
super();
mMockProviderName = mockProviderName;
mContext = context;
mHandler1 = handler;
// mGpsFakePointProvider = new ChangedPointProvider();
mGpsFakePointProvider = new StaticPointProvider();
}
public String getmMockProviderName() {
return mMockProviderName;
}
Runnable fakeProc = new Runnable() {
@Override
public void run() {
GpsFakePoint pnt = mGpsFakePointProvider.getGpsFakePoint();
setLocation(pnt.longitude, pnt.latitude);
mHandler1.postDelayed(fakeProc, mPostDelayed);
}
};
public void RunFake() {
mHandler1.postDelayed(fakeProc, mPostDelayed);
}
public void StopFake() {
mHandler1.removeCallbacks(fakeProc);
}
private void setLocation(double longitude, double latitude) {
LocationManager locationManager = (LocationManager) mContext
.getSystemService(Context.LOCATION_SERVICE);
if (locationManager.getProvider(mMockProviderName) == null) {
locationManager.addTestProvider(mMockProviderName, false, true,
false, false, false, false, false, 0, 5);
locationManager.setTestProviderEnabled(mMockProviderName, true);
}
Location loc = new Location(mMockProviderName);
loc.setTime(System.currentTimeMillis());
loc.setLatitude(latitude);
loc.setLongitude(longitude);
locationManager.setTestProviderLocation(mMockProviderName, loc);
Log.i("gps", String.format("once: x=%s y=%s", longitude, latitude));
}
public class GpsFakePoint {
public GpsFakePoint(double longitude, double latitude) {
super();
this.longitude = longitude;
this.latitude = latitude;
}
public double latitude;
public double longitude;
}
public interface GpsFakePointProvider {
public GpsFakePoint getGpsFakePoint();
}
public class ChangedPointProvider implements GpsFakePointProvider {
private double mLatitude = 1;
private double mLongitude = 1;
@Override
public GpsFakePoint getGpsFakePoint() {
return new GpsFakePoint(mLatitude++, mLongitude++);
}
}
public class StaticPointProvider implements GpsFakePointProvider {
public GpsFakePoint getGpsFakePoint() {
return new GpsFakePoint(116.9152, 39.4435);
//return new GpsFakePoint(116.8737, 39.4328);//偏左
//return new GpsFakePoint(116.886,39.4257);
}
}
}// end class GpsMockManager
在真机上 模拟GPS
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。