基于Xposed 拦截百度 高德地图 实现根据经纬度 改变当前位置

来源:互联网 发布:今日头条采集 帝国cms 编辑:程序博客网 时间:2024/06/06 05:44


由于项目需求  需要根据经纬度改变高德 百度地图当前的位置  

网上查了大量的资料  但最多也就能解决高德定位   没有能过百度地图  

 百度定位优先级 WIFI  基站  GPS    设置里 GPS 定位优先级选择只限制于 GPS  在进行修改基站定位 

有部分手机是可以过百度的  但大部分还是不行 而且基站位置并不好获取

 只有少部分网站能通过WIFI进行反追踪可以拿到 基站以及周边基站相关信息 但是每天接口只能获取到100条免费数据 在要多的话要花钱



下面的代码是最早的尝试版本 直接改基站位置 在把GPS 选项选择 最后一个 只限制于使用GPS定位 有一定小概率 加特定手机 会成功过百度地图  但很不稳定  比较简陋

XposedHelpers.findAndHookMethod(                "android.telephony.gsm.GsmCellLocation",                loadPkgParam.classLoader, "getLac", new XC_MethodHook() {                    @Override                    protected void afterHookedMethod(MethodHookParam param)                            throws Throwable {                        // TODO Auto-generated method stub                        super.afterHookedMethod(param);                             // SharedPreferences 传值 可直接填写 基站位置                        param.setResult(pre.getInt("Lat", 6));                    }                });        XposedHelpers.findAndHookMethod(                "android.telephony.gsm.GsmCellLocation",                loadPkgParam.classLoader, "getCid", new XC_MethodHook() {                    @Override                    protected void afterHookedMethod(MethodHookParam param)                            throws Throwable {                        // TODO Auto-generated method stub                        super.afterHookedMethod(param);                        param.setResult(pre.getInt("Cid", 6));                    }

既然百度是根据 WIFI 基站 GPS 信息定位的  WIFI MAC 基站位置 信号强弱等等我们都把这些改一遍 定位肯定能过 但太麻烦了 而且需要服务器大量的数据支持  所以我想到了可以把WIFI 基站统统设置为null 强制百度 高德只能获取到GPS信息进行定位 经过大量的尝试终于成功了   重点到了 直接贴代码  代码环境支eclipse AS 可以直接复制



public class GPShook {    public static void HookAndChange(ClassLoader classLoader, final double latitude, final double longtitude) {         // 基站信息设置为Null        XposedHelpers.findAndHookMethod("android.telephony.TelephonyManager", classLoader,                 "getCellLocation", new XC_MethodHook() {                     @Override                     protected void afterHookedMethod(MethodHookParam param) throws Throwable {                         param.setResult(null);                     }                     });         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.BASE) {             // 把基站信息设置为NULL             XposedHelpers.findAndHookMethod("android.telephony.TelephonyManager", classLoader,                     "getNeighboringCellInfo", new XC_MethodHook() {                         @Override                         protected void afterHookedMethod(MethodHookParam param) throws Throwable {                             param.setResult(null);                         }                     });         }         if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {             XposedHelpers.findAndHookMethod(TelephonyManager.class, "getAllCellInfo", new XC_MethodHook() {                 @Override                 protected void afterHookedMethod(MethodHookParam param) throws Throwable {                     param.setResult(null);                 }             });                        /* // WIFI 集合              XposedHelpers.findAndHookMethod("android.net.wifi.WifiManager", classLoader, "getScanResults", new XC_MethodHook() {                  @Override                  protected void afterHookedMethod(MethodHookParam param) throws Throwable {                      param.setResult(null);                  }              });         */                                                                     // 纬度             XposedHelpers.findAndHookMethod("android.location.Location", classLoader, "getLatitude", new XC_MethodHook() {                 @Override                 protected void beforeHookedMethod(MethodHookParam param)                         throws Throwable {                     // TODO Auto-generated method stub                     super.beforeHookedMethod(param);                                          param.setResult(latitude);                 }                              });                          // 经度             XposedHelpers.findAndHookMethod("android.location.Location", classLoader, "getLongitude", new XC_MethodHook() {                 @Override                 protected void beforeHookedMethod(MethodHookParam param)                         throws Throwable {                     // TODO Auto-generated method stub                     super.beforeHookedMethod(param);                     param.setResult(longtitude);                 }                              });                                                                                    XposedHelpers.findAndHookMethod("android.net.wifi.WifiInfo", classLoader, "getBSSID", new XC_MethodHook() {                  @Override                  protected void afterHookedMethod(MethodHookParam param) throws Throwable {                      param.setResult("00-00-00-00-00-00-00-00");                  }              });              XposedHelpers.findAndHookMethod("android.net.wifi.WifiInfo", classLoader, "getMacAddress", new XC_MethodHook() {                  @Override                  protected void afterHookedMethod(MethodHookParam param) throws Throwable {                      param.setResult("00-00-00-00-00-00-00-00");                  }              });                                            // WIFI 信息集合    /*                            XposedHelpers.findAndHookMethod("android.net.wifi.WifiInfo", classLoader, "getSSID", new XC_MethodHook() {               @Override               protected void afterHookedMethod(MethodHookParam param) throws Throwable {                   param.setResult("null");               }           });*//*      XposedHelpers.findAndHookMethod("android.net.wifi.WifiManager", classLoader, "getWifiState", new XC_MethodHook() {              @Override              protected void afterHookedMethod(MethodHookParam param) throws Throwable {                  param.setResult(1);              }          });                      XposedHelpers.findAndHookMethod("android.net.NetworkInfo", classLoader,                 "getTypeName", new XC_MethodHook() {                     @Override                     protected void afterHookedMethod(MethodHookParam param) throws Throwable {                         param.setResult("WIFI");                     }                 });*/        XposedHelpers.findAndHookMethod("android.net.wifi.WifiManager", classLoader, "isWifiEnabled", new XC_MethodHook() {            @Override            protected void afterHookedMethod(MethodHookParam param) throws Throwable {                param.setResult(false);            }        });            /*     XposedHelpers.findAndHookMethod("android.net.NetworkInfo", classLoader,                 "isConnectedOrConnecting", new XC_MethodHook() {                     @Override                     protected void afterHookedMethod(MethodHookParam param) throws Throwable {                         param.setResult(false);                     }                 });                  XposedHelpers.findAndHookMethod("android.net.NetworkInfo", classLoader,                 "isConnected", new XC_MethodHook() {                     @Override                     protected void afterHookedMethod(MethodHookParam param) throws Throwable {                         param.setResult(false);                     }                 });        XposedHelpers.findAndHookMethod("android.net.NetworkInfo", classLoader,                 "isAvailable", new XC_MethodHook() {                     @Override                     protected void afterHookedMethod(MethodHookParam param) throws Throwable {                         param.setResult(false);                     }                 });         XposedHelpers.findAndHookMethod("android.telephony.CellInfo", classLoader,                 "isRegistered", new XC_MethodHook() {                     @Override                     protected void afterHookedMethod(MethodHookParam param) throws Throwable {                         param.setResult(false);                     }                 });*/                //判断该LocationProvider是否需要访问网络基站        XposedHelpers.findAndHookMethod(LocationManager.class, "requiresCell",  new XC_MethodHook() {            @Override            protected void afterHookedMethod(MethodHookParam param) throws Throwable {                param.setResult(false);            }        });        //判断该LocationProvider是否需要网络数据        XposedHelpers.findAndHookMethod(LocationManager.class, "requiresNetwork",  new XC_MethodHook() {            @Override            protected void afterHookedMethod(MethodHookParam param) throws Throwable {                param.setResult(false);            }        });                        XposedHelpers.findAndHookMethod(LocationManager.class, "getLastLocation", new XC_MethodHook() {            @Override            protected void afterHookedMethod(MethodHookParam param) throws Throwable {                Location l = new Location(LocationManager.GPS_PROVIDER);                l.setLatitude(latitude);                l.setLongitude(longtitude);                l.setAccuracy(100f);                l.setTime(0);                /*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {                    l.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());                }*/                param.setResult(l);            }        });        XposedHelpers.findAndHookMethod(LocationManager.class, "getLastKnownLocation", String.class, new XC_MethodHook() {            @Override            protected void afterHookedMethod(MethodHookParam param) throws Throwable {                Location l = new Location(LocationManager.GPS_PROVIDER);                l.setLatitude(latitude);                l.setLongitude(longtitude);                l.setAccuracy(100f);                l.setTime(0);                /*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {                    l.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());                }*/                param.setResult(l);            }        });        XposedBridge.hookAllMethods(LocationManager.class, "getProviders", new XC_MethodHook() {            @Override            protected void afterHookedMethod(MethodHookParam param) throws Throwable {                ArrayList<String> arrayList = new ArrayList<String>();                arrayList.add("gps");                param.setResult(arrayList);            }        });        XposedHelpers.findAndHookMethod(LocationManager.class, "getBestProvider", Criteria.class, Boolean.TYPE, new XC_MethodHook() {            @Override            protected void afterHookedMethod(MethodHookParam param) throws Throwable {                param.setResult("gps");            }        });        XposedHelpers.findAndHookMethod(LocationManager.class, "addGpsStatusListener", GpsStatus.Listener.class, new XC_MethodHook() {            @Override            protected void afterHookedMethod(MethodHookParam param) throws Throwable {                if (param.args[0] != null) {                    XposedHelpers.callMethod(param.args[0], "onGpsStatusChanged", 1);                    XposedHelpers.callMethod(param.args[0], "onGpsStatusChanged", 3);                }            }        });        XposedHelpers.findAndHookMethod(LocationManager.class, "addNmeaListener", GpsStatus.NmeaListener.class, new XC_MethodHook() {            @Override            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {                param.setResult(false);            }        });        XposedHelpers.findAndHookMethod("android.location.LocationManager", classLoader,                "getGpsStatus", GpsStatus.class, new XC_MethodHook() {                    @Override                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {                        GpsStatus gss = (GpsStatus) param.getResult();                        if (gss == null)                            return;                        Class<?> clazz = GpsStatus.class;                        Method m = null;                        for (Method method : clazz.getDeclaredMethods()) {                            if (method.getName().equals("setStatus")) {                                if (method.getParameterTypes().length > 1) {                                    m = method;                                    break;                                }                            }                        }                        if (m == null)                            return;                        //access the private setStatus function of GpsStatus                        m.setAccessible(true);                        //make the apps belive GPS works fine now                        int svCount = 5;                        int[] prns = {1, 2, 3, 4, 5};                        float[] snrs = {0, 0, 0, 0, 0};                        float[] elevations = {0, 0, 0, 0, 0};                        float[] azimuths = {0, 0, 0, 0, 0};                        int ephemerisMask = 0x1f;                        int almanacMask = 0x1f;                        //5 satellites are fixed                        int usedInFixMask = 0x1f;                        XposedHelpers.callMethod(gss, "setStatus", svCount, prns, snrs, elevations, azimuths, ephemerisMask, almanacMask, usedInFixMask);                        param.args[0] = gss;                        param.setResult(gss);                        try {                            m.invoke(gss, svCount, prns, snrs, elevations, azimuths, ephemerisMask, almanacMask, usedInFixMask);                            param.setResult(gss);                        } catch (Exception e) {                            XposedBridge.log(e);                        }                    }                });        for (Method method : LocationManager.class.getDeclaredMethods()) {            if (method.getName().equals("requestLocationUpdates")                    && !Modifier.isAbstract(method.getModifiers())                    && Modifier.isPublic(method.getModifiers())) {                XposedBridge.hookMethod(method, new XC_MethodHook() {                    @Override                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {                        if (param.args.length >= 4 && (param.args[3] instanceof LocationListener)) {                            LocationListener ll = (LocationListener) param.args[3];                            Class<?> clazz = LocationListener.class;                            Method m = null;                            for (Method method : clazz.getDeclaredMethods()) {                                if (method.getName().equals("onLocationChanged") && !Modifier.isAbstract(method.getModifiers())) {                                    m = method;                                    break;                                }                            }                            Location l = new Location(LocationManager.GPS_PROVIDER);                            l.setLatitude(latitude);                            l.setLongitude(longtitude);                            l.setAccuracy(10.00f);                            l.setTime(0);                        /*    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {                                l.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());                            }*/                            XposedHelpers.callMethod(ll, "onLocationChanged", l);                            try {                                if (m != null) {                                    m.invoke(ll, l);                                }                            } catch (Exception e) {                                XposedBridge.log(e);                            }                        }                    }                });            }            if (method.getName().equals("requestSingleUpdate ")                    && !Modifier.isAbstract(method.getModifiers())                    && Modifier.isPublic(method.getModifiers())) {                XposedBridge.hookMethod(method, new XC_MethodHook() {                    @Override                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {                        if (param.args.length >= 3 && (param.args[1] instanceof LocationListener)) {                            LocationListener ll = (LocationListener) param.args[3];                            Class<?> clazz = LocationListener.class;                            Method m = null;                            for (Method method : clazz.getDeclaredMethods()) {                                if (method.getName().equals("onLocationChanged") && !Modifier.isAbstract(method.getModifiers())) {                                    m = method;                                    break;                                }                            }                            try {                                if (m != null) {                                    Location l = new Location(LocationManager.GPS_PROVIDER);                                    l.setLatitude(latitude);                                    l.setLongitude(longtitude);                                                                    l.setAccuracy(100f);                                    l.setTime(0);                                /*    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {                                        l.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());                                    }*/                                    m.invoke(ll, l);                                }                            } catch (Exception e) {                                XposedBridge.log(e);                            }                        }                    }                });            }        }             }    }}


我把代码直接封装成一个工具类 直接用就行   青橙手机成功改变当前位置 我在常州用百度定的位置 显示在北京



估计可能有人会问代码怎么用  在handleLoadPackage   我做了包名判断 只对百度高德生效 因为有些函数比较敏感 全局会对手机造成 各种不稳定   有些手机可能没事  但很大几率 com.anroid.phone 会造成崩溃的   

 

        if (sharePkgParam.packageName.equals("com.baidu.BaiduMap")||                sharePkgParam.packageName.equals("com.autonavi.minimap")) {                //               可以直接 添加经纬度            GPShook.HookAndChange(sharePkgParam.classLoader,pre.getFloat("lat",0),pre.getFloat("log",0));                        }


最后附上GPS 相关API  http://www.cnblogs.com/android-blogs/p/5718479.html






至于修改硬件码等函数的博客 我会重新写   图片的形式很不适合阅读 我会改进下 网上大量开源代码 资料博客 帮助我们很多 写博客 一是为了记录总结项目的思路  也是为了帮助更多的人






1 0
原创粉丝点击