android如何用模拟的gps
来源:互联网 发布:单片机原理图怎么看 编辑:程序博客网 时间:2024/04/28 00:12
准备工作:我们需要在我们手机上设置允许模拟:
设置
---》应用程序--》开发---》模拟测试地点开启
(就在开发者选项里面允许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
代码大致如下
- 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坐标给他
- 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);
- }
- 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(){
- //加这个类似你在浏览器里面同意分享你的位置
- 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
0 0
- android如何用模拟的gps
- android 自定义webview 如何使用gps 如何用模拟的gps
- 如何用TComboBox模拟TColorBox
- 如何用SendMessage模拟某一按钮的点击事件
- 如何用SendMessage模拟某一按钮的点击事件
- 如何用SendMessage模拟某一按钮的点击事件
- 【转】如何用SendMessage模拟某一按钮的点击事件
- 菜鸟如何用servlet模拟一个简单的登陆
- 如何用Machinations示意图来模拟《吃豆人》的游戏机制?
- 如何用servlet模拟一个简单的登陆
- 转发:如何用SendMessage模拟某一按钮的点击事件
- 如何用SendMessage模拟某一按钮的点击事件
- 如何用GPS找回丢失手机是所在位置
- 如何用Modis模拟WAP上网
- 如何用php实现模拟微信抢红包
- 如何用两个栈模拟队列操作
- 如何用android 画出三角形
- 如何用android 画出三角形
- error Unknow type name "NSString"
- C++---multiset
- 浅析live555媒体库之自己实现文件流的读写
- 2015.9.13 关于数据结构
- 系统UIRefreshControl 的简单使用代码
- android如何用模拟的gps
- 矩阵链乘法问题描述(Matrix-chain multiplication)
- 布线问题 38 (最小生成树 prim)
- Android Studio系列-签名打包
- ACM比赛需知
- js面向对象的例子(私有/公有/特权方法)-2
- C语言,文件操作
- 菜单栏UITabBarController
- 代码篇C#网络编程之局域网聊天