android如何用模拟的gps
2015-09-13 14:05
477 查看
准备工作:我们需要在我们手机上设置允许模拟:
设置
---》应用程序--》开发---》模拟测试地点开启
(就在开发者选项里面允许debug的下面)
然后声明权限:
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
1 如何fake gps ?
gps的fake 有个很奇怪的现象 你需要把fake gps的代码放到一个service当中 不知道是否是系统对发出fake信息的源进行了限定 目前实验结果是需要放在service
代码大致如下
[java] view
plaincopy
package com.yiqiding.ktvbox.view.service;
import java.lang.reflect.Method;
import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import com.yiqiding.ktvbox.util.LogUtil;
public class GpsFakeService extends Service {
private static final String LOG_TAG = "GpsFakeService";
private float accuracy;
private double altitude;
private float bearing;
private Bundle bl;
private boolean forFlag = true;
private Handler handler = new Handler();
private double lat;
private double lng;
private LocationManager mLocationManager;
private Runnable runnable = new Runnable() {
public void run() {
try {
mLocationManager.sendExtraCommand("gps",
"force_xtra_injection", bl);
mLocationManager.sendExtraCommand("gps",
"force_time_injection", bl);
Location localLocation = getLoc("gps");
mLocationManager.setTestProviderLocation("gps", localLocation);
LogUtil.v("set localcation" + localLocation);
handler.postDelayed(this, 1000L);
} catch (Exception exception) {
exception.printStackTrace();
}
}
};
private float speed;
private Location getLoc(String paramString) {
Location localLocation = new Location(paramString);
localLocation.setLatitude(lat);
localLocation.setLongitude(lng);
localLocation.setAltitude(altitude);
localLocation.setBearing(bearing);
localLocation.setSpeed(speed);
localLocation.setAccuracy(accuracy);
localLocation.setTime(System.currentTimeMillis());
try {
Method method = Location.class.getMethod("makeComplete");
if (method != null) {
method.invoke(localLocation);
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return localLocation;
}
private void removeProvider() {
try {
mLocationManager.removeTestProvider("gps");
} catch (Exception exception) {
Log.e(LOG_TAG, exception.getMessage());
}
}
public IBinder onBind(Intent paramIntent) {
return null;
}
public void onCreate() {
super.onCreate();
}
public void onDestroy() {
super.onDestroy();
removeProvider();
try {
handler.removeCallbacks(runnable);
} catch (Exception exception) {
exception.printStackTrace();
}
}
public void onStart(Intent paramIntent, int paramInt) {
super.onStart(paramIntent, paramInt);
}
public int onStartCommand(Intent paramIntent, int paramInt1, int paramInt2) {
LogUtil.i("will fetch locationManager then set location");
mLocationManager = ((LocationManager) getSystemService("location"));
mLocationManager.addTestProvider("gps", false, false, false,
false, false, false, false, 0, 0);
mLocationManager.setTestProviderEnabled("gps", true);
bl = paramIntent.getExtras();
if (bl != null) {
if (bl.containsKey("lat"))
lat = paramIntent.getDoubleExtra("lat", 0.0D);
if (bl.containsKey("lng"))
lng = paramIntent.getDoubleExtra("lng", 0.0D);
if (bl.containsKey("accuracy"))
accuracy = paramIntent.getFloatExtra("accuracy", 0.0F);
handler.postDelayed(runnable, 100L);
}
return START_REDELIVER_INTENT;
}
}
然后你只需要发送要fake的gps坐标给他
[java] view
plaincopy
private void startTestGps(){
LogUtil.i("will start gpsFakeService");
Intent mIntent = new Intent(this, GpsFakeService.class);
mIntent.putExtra("lat", 31.12121245);
mIntent.putExtra("lng", 121.124546461);
mIntent.putExtra("accuracy", 5.0f);
mIntent.putExtra("bearing", 0.0f);
mIntent.putExtra("speed", 10.0f);
startService(mIntent);
}
//结束时候要注意关闭fake服务
private void endTestGps(){
LogUtil.i("will stop gpsFakeService");
Intent mIntent = new Intent(this, GpsFakeService.class);
stopService(mIntent);
}
2 如何让自定义的webview能调用gps
[java] view
plaincopy
WebView mWebView = (WebView) dacheViewRoot.findViewById(R.id.webView1);
mWebView.getSettings().setJavaScriptEnabled(true);//启用支持javascript
mWebView.getSettings().setDomStorageEnabled(true);//加这个是为了解决打开页面时候有解析报错问题
mWebView.getSettings().setGeolocationEnabled(true);//支持geo
mWebView.loadUrl("你的需要调用gps功能的网页");
mWebView.setWebChromeClient(new WebChromeClient(){
[java] view
plaincopy
//加这个类似你在浏览器里面同意分享你的位置
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
LogUtil.i("we allow geo location permission");
callback.invoke(origin, true, false);
}
});
src:http://blog.csdn.net/tiantianshangcha/article/details/25277361?utm_source=tuicool
http://www.eoeandroid.com/thread-74548-1-1.html?_dsign=dd465fd1
设置
---》应用程序--》开发---》模拟测试地点开启
(就在开发者选项里面允许debug的下面)
然后声明权限:
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
1 如何fake gps ?
gps的fake 有个很奇怪的现象 你需要把fake gps的代码放到一个service当中 不知道是否是系统对发出fake信息的源进行了限定 目前实验结果是需要放在service
代码大致如下
[java] view
plaincopy
package com.yiqiding.ktvbox.view.service;
import java.lang.reflect.Method;
import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import com.yiqiding.ktvbox.util.LogUtil;
public class GpsFakeService extends Service {
private static final String LOG_TAG = "GpsFakeService";
private float accuracy;
private double altitude;
private float bearing;
private Bundle bl;
private boolean forFlag = true;
private Handler handler = new Handler();
private double lat;
private double lng;
private LocationManager mLocationManager;
private Runnable runnable = new Runnable() {
public void run() {
try {
mLocationManager.sendExtraCommand("gps",
"force_xtra_injection", bl);
mLocationManager.sendExtraCommand("gps",
"force_time_injection", bl);
Location localLocation = getLoc("gps");
mLocationManager.setTestProviderLocation("gps", localLocation);
LogUtil.v("set localcation" + localLocation);
handler.postDelayed(this, 1000L);
} catch (Exception exception) {
exception.printStackTrace();
}
}
};
private float speed;
private Location getLoc(String paramString) {
Location localLocation = new Location(paramString);
localLocation.setLatitude(lat);
localLocation.setLongitude(lng);
localLocation.setAltitude(altitude);
localLocation.setBearing(bearing);
localLocation.setSpeed(speed);
localLocation.setAccuracy(accuracy);
localLocation.setTime(System.currentTimeMillis());
try {
Method method = Location.class.getMethod("makeComplete");
if (method != null) {
method.invoke(localLocation);
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return localLocation;
}
private void removeProvider() {
try {
mLocationManager.removeTestProvider("gps");
} catch (Exception exception) {
Log.e(LOG_TAG, exception.getMessage());
}
}
public IBinder onBind(Intent paramIntent) {
return null;
}
public void onCreate() {
super.onCreate();
}
public void onDestroy() {
super.onDestroy();
removeProvider();
try {
handler.removeCallbacks(runnable);
} catch (Exception exception) {
exception.printStackTrace();
}
}
public void onStart(Intent paramIntent, int paramInt) {
super.onStart(paramIntent, paramInt);
}
public int onStartCommand(Intent paramIntent, int paramInt1, int paramInt2) {
LogUtil.i("will fetch locationManager then set location");
mLocationManager = ((LocationManager) getSystemService("location"));
mLocationManager.addTestProvider("gps", false, false, false,
false, false, false, false, 0, 0);
mLocationManager.setTestProviderEnabled("gps", true);
bl = paramIntent.getExtras();
if (bl != null) {
if (bl.containsKey("lat"))
lat = paramIntent.getDoubleExtra("lat", 0.0D);
if (bl.containsKey("lng"))
lng = paramIntent.getDoubleExtra("lng", 0.0D);
if (bl.containsKey("accuracy"))
accuracy = paramIntent.getFloatExtra("accuracy", 0.0F);
handler.postDelayed(runnable, 100L);
}
return START_REDELIVER_INTENT;
}
}
然后你只需要发送要fake的gps坐标给他
[java] view
plaincopy
private void startTestGps(){
LogUtil.i("will start gpsFakeService");
Intent mIntent = new Intent(this, GpsFakeService.class);
mIntent.putExtra("lat", 31.12121245);
mIntent.putExtra("lng", 121.124546461);
mIntent.putExtra("accuracy", 5.0f);
mIntent.putExtra("bearing", 0.0f);
mIntent.putExtra("speed", 10.0f);
startService(mIntent);
}
//结束时候要注意关闭fake服务
private void endTestGps(){
LogUtil.i("will stop gpsFakeService");
Intent mIntent = new Intent(this, GpsFakeService.class);
stopService(mIntent);
}
2 如何让自定义的webview能调用gps
[java] view
plaincopy
WebView mWebView = (WebView) dacheViewRoot.findViewById(R.id.webView1);
mWebView.getSettings().setJavaScriptEnabled(true);//启用支持javascript
mWebView.getSettings().setDomStorageEnabled(true);//加这个是为了解决打开页面时候有解析报错问题
mWebView.getSettings().setGeolocationEnabled(true);//支持geo
mWebView.loadUrl("你的需要调用gps功能的网页");
mWebView.setWebChromeClient(new WebChromeClient(){
[java] view
plaincopy
//加这个类似你在浏览器里面同意分享你的位置
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
LogUtil.i("we allow geo location permission");
callback.invoke(origin, true, false);
}
});
src:http://blog.csdn.net/tiantianshangcha/article/details/25277361?utm_source=tuicool
http://www.eoeandroid.com/thread-74548-1-1.html?_dsign=dd465fd1
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories