Android开发实现模拟定位

2019-12-31  本文已影响0人  beizhi

此功能是基于开发者模式中的模拟位置信息应用实现,目前基本实现点击地图设置虚拟定位功能,唯一遗憾的是目前会被第三方 app检测出模拟定位,更多的功能会一步步迭代,此文章只用于学习记录,注:此功能借鉴了github上的部分项目。

模拟定位的实现步骤如下:

1)安卓6.0以上 打开开发者模式→模拟位置信息应用

2)安卓6.0以前 打开开发者模式→允许模拟位置

3)调用Android原生SDK提供方法

代码如下:

LocationProvider provider =locationManager.getProvider(LocationManager.GPS_PROVIDER);

try {

locationManager.addTestProvider(LocationManager.GPS_PROVIDER, false, true, true,

            false, true, true, true, 0, 5);

}catch (Exception e){

e.printStackTrace();

}

if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

try {

locationManager.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);

    }catch (Exception e){

e.printStackTrace();

    }

}

try{

locationManager.setTestProviderStatus(LocationManager.GPS_PROVIDER, LocationProvider.AVAILABLE, null,

            System.currentTimeMillis());

}catch (Exception e){

}

设置经纬度的方法如下

locationManager.setTestProviderLocation(providerStr, generateLocation(latLng));


public Location generateLocation(LatLng latLng) {
        Location loc = new Location("gps");


        loc.setAccuracy(2.0F);
        loc.setAltitude(55.0D);
        loc.setBearing(1.0F);
        Bundle bundle = new Bundle();
        bundle.putInt("satellites", 7);
        loc.setExtras(bundle);


        loc.setLatitude(latLng.latitude);
        loc.setLongitude(latLng.longitude);

        loc.setTime(System.currentTimeMillis());
        if (Build.VERSION.SDK_INT >= 17) {
            loc.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
        }
        return loc;
    }

最重要的就是需要判断有没有打开开发者模式和开启虚拟定位,代码如下:

package com.example.dell.virtualgps.tools;

import android.content.Context;
import android.location.Criteria;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Build;
import android.provider.Settings;

/**
 * Time:2019/12/30
 * Author:
 * Description:
 */
public class AllowMockLocationTools {
    private static AllowMockLocationTools singleton;
    private Context mContext;
    public AllowMockLocationTools(Context context){
        this.mContext=context;
    }
    public static AllowMockLocationTools with(Context context) {
        if (singleton == null) {
            synchronized (LocalTools.class) {
                if (singleton == null) {
                    singleton = new Builder(context).build();
                }
            }
        }
        return singleton;
    }

    //模拟位置权限是否开启
    public boolean isAllowMockLocation() {
        boolean canMockPosition = false;
        if (Build.VERSION.SDK_INT <= 22) {//6.0以下
            canMockPosition = Settings.Secure.getInt(mContext.getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION, 0) != 0;
        } else {
            try {
                LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);//获得LocationManager引用
                String providerStr = LocationManager.GPS_PROVIDER;
                LocationProvider provider = locationManager.getProvider(providerStr);
                if (provider != null) {
                    locationManager.addTestProvider(
                            provider.getName()
                            , provider.requiresNetwork()
                            , provider.requiresSatellite()
                            , provider.requiresCell()
                            , provider.hasMonetaryCost()
                            , provider.supportsAltitude()
                            , provider.supportsSpeed()
                            , provider.supportsBearing()
                            , provider.getPowerRequirement()
                            , provider.getAccuracy());
                } else {
                    locationManager.addTestProvider(
                            providerStr
                            , true, true, false, false, true, true, true
                            , Criteria.POWER_HIGH, Criteria.ACCURACY_FINE);
                }
                locationManager.setTestProviderEnabled(providerStr, true);
                locationManager.setTestProviderStatus(providerStr, LocationProvider.AVAILABLE, null, System.currentTimeMillis());
                // 模拟位置可用
                canMockPosition = true;
                locationManager.setTestProviderEnabled(providerStr, false);
                locationManager.removeTestProvider(providerStr);
            } catch (SecurityException e) {
                canMockPosition = false;
            }
        }
        return canMockPosition;
    }

    public static class Builder{
        private Context context;
        public Builder(Context context) {
            if (context == null) {
                throw new IllegalArgumentException("Context cannot be empty");
            }
            this.context = context.getApplicationContext();
        }

        public AllowMockLocationTools build() {
            return new AllowMockLocationTools(context);
        }
    }
}

接下来就是清单文件的配置,你需要申请定位权限,配置如下:

   <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
    </uses-permission>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
    </uses-permission>
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" >
    </uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
    </uses-permission>
    <uses-permission android:name="android.permission.INTERNET" >
    </uses-permission>
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
        tools:ignore="ProtectedPermissions">
    </uses-permission>
    <uses-permission android:name="android.permission.READ_LOGS"
        tools:ignore="ProtectedPermissions">
    </uses-permission>
    <uses-permission android:name="android.permission.VIBRATE" >
    </uses-permission>
    <uses-permission android:name="android.permission.WAKE_LOCK" >
    </uses-permission>
    <uses-permission android:name="android.permission.WRITE_SETTINGS"
        tools:ignore="ProtectedPermissions">
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"
        tools:ignore="MockLocation,ProtectedPermissions">
    </uses-permission>
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW">
    </uses-permission>
<!-- 这个权限用于进行网络定位 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- 这个权限用于访问GPS定位 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- 获取网络状态,根据网络状态切换进行数据请求网络转换 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- 写外置存储。如果开发者使用了离线地图,并且数据写在外置存储区域,则需要申请该权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 读取外置存储。如果开发者使用了so动态加载功能并且把so文件放在了外置存储区域,则需要申请该权限,否则不需要 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- 访问网络,进行地图相关业务数据请求,包括地图数据,路线规划,POI检索等 -->
<uses-permission android:name="android.permission.INTERNET" />
上一篇下一篇

猜你喜欢

热点阅读