日期:2014-05-16  浏览次数:20800 次

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

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 =