標籤:android des style blog class code
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);}});