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

代码大致如下 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.yiqiding.ktvbox.view.service;  
  2.   
  3. import java.lang.reflect.Method;  
  4.   
  5. import android.app.Service;  
  6. import android.content.Intent;  
  7. import android.location.Location;  
  8. import android.location.LocationManager;  
  9. import android.os.Bundle;  
  10. import android.os.Handler;  
  11. import android.os.IBinder;  
  12. import android.util.Log;  
  13.   
  14. import com.yiqiding.ktvbox.util.LogUtil;  
  15.   
  16. public class GpsFakeService extends Service {  
  17.     private static final String LOG_TAG = "GpsFakeService";  
  18.     private float accuracy;  
  19.     private double altitude;  
  20.     private float bearing;  
  21.     private Bundle bl;  
  22.     private boolean forFlag = true;  
  23.     private Handler handler = new Handler();  
  24.     private double lat;  
  25.     private double lng;  
  26.     private LocationManager mLocationManager;  
  27.     private Runnable runnable = new Runnable() {  
  28.         public void run() {  
  29.             try {  
  30.                 mLocationManager.sendExtraCommand("gps",  
  31.                         "force_xtra_injection", bl);  
  32.                 mLocationManager.sendExtraCommand("gps",  
  33.                         "force_time_injection", bl);  
  34.                 Location localLocation = getLoc("gps");  
  35.                 mLocationManager.setTestProviderLocation("gps", localLocation);  
  36.                 LogUtil.v("set localcation" + localLocation);  
  37.                 handler.postDelayed(this, 1000L);  
  38.             } catch (Exception exception) {  
  39.                 exception.printStackTrace();  
  40.             }  
  41.         }  
  42.     };  
  43.   
  44.     private float speed;  
  45.   
  46.     private Location getLoc(String paramString) {  
  47.         Location localLocation = new Location(paramString);  
  48.         localLocation.setLatitude(lat);  
  49.         localLocation.setLongitude(lng);  
  50.         localLocation.setAltitude(altitude);  
  51.         localLocation.setBearing(bearing);  
  52.         localLocation.setSpeed(speed);  
  53.         localLocation.setAccuracy(accuracy);  
  54.         localLocation.setTime(System.currentTimeMillis());  
  55.         try {  
  56.             Method method = Location.class.getMethod("makeComplete");  
  57.             if (method != null) {  
  58.                 method.invoke(localLocation);  
  59.             }  
  60.         } catch (NoSuchMethodException e) {  
  61.             e.printStackTrace();  
  62.         } catch (Exception e) {  
  63.             e.printStackTrace();  
  64.         }  
  65.         return localLocation;  
  66.     }  
  67.   
  68.     private void removeProvider() {  
  69.         try {  
  70.             mLocationManager.removeTestProvider("gps");  
  71.         } catch (Exception exception) {  
  72.             Log.e(LOG_TAG, exception.getMessage());  
  73.         }  
  74.     }  
  75.   
  76.     public IBinder onBind(Intent paramIntent) {  
  77.         return null;  
  78.     }  
  79.   
  80.     public void onCreate() {  
  81.         super.onCreate();  
  82.     }  
  83.   
  84.     public void onDestroy() {  
  85.         super.onDestroy();  
  86.         removeProvider();  
  87.         try {  
  88.             handler.removeCallbacks(runnable);  
  89.         } catch (Exception exception) {  
  90.             exception.printStackTrace();  
  91.         }  
  92.     }  
  93.   
  94.     public void onStart(Intent paramIntent, int paramInt) {  
  95.         super.onStart(paramIntent, paramInt);  
  96.     }  
  97.   
  98.     public int onStartCommand(Intent paramIntent, int paramInt1, int paramInt2) {  
  99.         LogUtil.i("will fetch locationManager then set location");  
  100.         mLocationManager = ((LocationManager) getSystemService("location"));  
  101.         mLocationManager.addTestProvider("gps"falsefalsefalse,  
  102.                 falsefalsefalsefalse00);  
  103.         mLocationManager.setTestProviderEnabled("gps"true);  
  104.         bl = paramIntent.getExtras();  
  105.         if (bl != null) {  
  106.             if (bl.containsKey("lat"))  
  107.                 lat = paramIntent.getDoubleExtra("lat"0.0D);  
  108.             if (bl.containsKey("lng"))  
  109.                 lng = paramIntent.getDoubleExtra("lng"0.0D);  
  110.             if (bl.containsKey("accuracy"))  
  111.                 accuracy = paramIntent.getFloatExtra("accuracy"0.0F);  
  112.             handler.postDelayed(runnable, 100L);  
  113.         }  
  114.         return START_REDELIVER_INTENT;  
  115.     }  
  116. }  


然后你只需要发送要fake的gps坐标给他


[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. private void startTestGps(){  
  2.         LogUtil.i("will start gpsFakeService");  
  3.         Intent mIntent = new  Intent(this, GpsFakeService.class);  
  4.         mIntent.putExtra("lat"31.12121245);  
  5.         mIntent.putExtra("lng"121.124546461);  
  6.         mIntent.putExtra("accuracy"5.0f);  
  7.         mIntent.putExtra("bearing"0.0f);  
  8.         mIntent.putExtra("speed"10.0f);  
  9.         startService(mIntent);  
  10.     }  
  11.     //结束时候要注意关闭fake服务  
  12.     private void endTestGps(){  
  13.         LogUtil.i("will stop gpsFakeService");  
  14.         Intent mIntent = new  Intent(this, GpsFakeService.class);  
  15.         stopService(mIntent);  
  16.     }  


2 如何让自定义的webview能调用gps

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. WebView mWebView = (WebView) dacheViewRoot.findViewById(R.id.webView1);  
  2.         mWebView.getSettings().setJavaScriptEnabled(true);//启用支持javascript  
  3.         mWebView.getSettings().setDomStorageEnabled(true);//加这个是为了解决打开页面时候有解析报错问题  
  4.         mWebView.getSettings().setGeolocationEnabled(true);//支持geo  
  5.         mWebView.loadUrl("你的需要调用gps功能的网页");  
  6.         mWebView.setWebChromeClient(new WebChromeClient(){  
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //加这个类似你在浏览器里面同意分享你的位置  
  2.             public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {  
  3.                 LogUtil.i("we allow geo location permission");  
  4.                 callback.invoke(origin, truefalse);  
  5.             }  
  6.         });  



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