android 自定义webview 如何使用gps 如何用模拟的gps

来源:互联网 发布:java开发工作经历描述 编辑:程序博客网 时间:2024/05/01 07:55

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);}


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

WebView mWebView = (WebView) dacheViewRoot.findViewById(R.id.webView1);mWebView.getSettings().setJavaScriptEnabled(true);//启用支持javascriptmWebView.getSettings().setDomStorageEnabled(true);//加这个是为了解决打开页面时候有解析报错问题mWebView.getSettings().setGeolocationEnabled(true);//支持geomWebView.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);}});




0 0
原创粉丝点击