一栖秕、項(xiàng)目git地址:
??https://github.com/XieXiePro/MockLocation
二傍菇、實(shí)現(xiàn)原理:
??手機(jī)定位方式目前有4種:基站定位惨恭,WIFI定位,GPS定位丹鸿,AGPS定位缸血。
??本工程利用手機(jī)自帶的"模擬位置"功能實(shí)現(xiàn)運(yùn)行時(shí)修改LocationManager結(jié)果澜汤。
??原理:使用android自帶的調(diào)試api,模擬gps provider的結(jié)果敢艰。
??Android 6.0系統(tǒng)以下诬乞,可以通過Setting.Secure.ALLOW_MOCK_LOCATION獲取是否【允許模擬位置】,當(dāng)【允許模擬位置】開啟時(shí),可addTestProvider震嫉;
??Android 6.0系統(tǒng)及以上森瘪,棄用Setting.Secure.ALLOW_MOCK_LOCATION變量,沒有【允許模擬位置】選項(xiàng)票堵,
增加【選擇模擬位置信息應(yīng)用】扼睬,此時(shí)需要選擇當(dāng)前應(yīng)用,才可以addTestProvider换衬,
但未找到獲取當(dāng)前選擇應(yīng)用的方法痰驱,因此通過addTestProvider是否成功來(lái)判斷是否可用模擬位置。
三瞳浦、代碼分析:
MockLocationManager:模擬地址管理類
??首先通過Android系統(tǒng)模擬位置管理器LocationManager獲取系統(tǒng)模擬位置服務(wù)担映,Android 6.0以下,通過Setting.Secure.ALLOW_MOCK_LOCATION判斷是否可模擬位置叫潦,Android 6.0及以上蝇完,需要【選擇模擬位置信息應(yīng)用】,未找到方法矗蕊,因此通過addTestProvider是否可用判斷短蜕。
/**
* 模擬位置是否啟用
* 若啟用,則addTestProvider
*/
public boolean getUseMockPosition(Context context) {
// Android 6.0以下傻咖,通過Setting.Secure.ALLOW_MOCK_LOCATION判斷
// Android 6.0及以上朋魔,需要【選擇模擬位置信息應(yīng)用】,未找到方法卿操,因此通過addTestProvider是否可用判斷
boolean canMockPosition = (Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION, 0) != 0)
|| Build.VERSION.SDK_INT > 22;
if (canMockPosition && hasAddTestProvider == false) {
try {
for (String providerStr : mockProviders) {
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 {
if (providerStr.equals(LocationManager.GPS_PROVIDER)) {
locationManager.addTestProvider(
providerStr
, true, true, false, false, true, true, true
, Criteria.POWER_HIGH, Criteria.ACCURACY_FINE);
} else if (providerStr.equals(LocationManager.NETWORK_PROVIDER)) {
locationManager.addTestProvider(
providerStr
, true, false, true, false, false, false, false
, Criteria.POWER_LOW, Criteria.ACCURACY_FINE);
} else {
locationManager.addTestProvider(
providerStr
, false, false, false, false, true, true, true
, Criteria.POWER_LOW, Criteria.ACCURACY_FINE);
}
}
locationManager.setTestProviderEnabled(providerStr, true);
locationManager.setTestProviderStatus(providerStr, LocationProvider.AVAILABLE, null, System.currentTimeMillis());
}
hasAddTestProvider = true; // 模擬位置可用
canMockPosition = true;
} catch (SecurityException e) {
canMockPosition = false;
}
}
if (canMockPosition == false) {
stopMockLocation();
}
return canMockPosition;
}
??接下來(lái)設(shè)置模擬經(jīng)緯度數(shù)據(jù):
// 模擬位置(addTestProvider成功的前提下)
for (String providerStr : mockProviders) {
Location mockLocation = new Location(providerStr);
mockLocation.setLatitude(latitude); // 維度(度)
mockLocation.setLongitude(longitude); // 經(jīng)度(度)
mockLocation.setAccuracy(0.1f); // 精度(米)
mockLocation.setTime(new Date().getTime()); // 本地時(shí)間
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
mockLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
}
locationManager.setTestProviderLocation(providerStr, mockLocation);
}
??取消模擬定位方法:
/**
* 取消位置模擬警检,以免啟用模擬數(shù)據(jù)后無(wú)法還原使用系統(tǒng)位置
* 若模擬位置未開啟,則removeTestProvider將會(huì)拋出異常害淤;
* 若已a(bǔ)ddTestProvider后扇雕,關(guān)閉模擬位置,未removeTestProvider將導(dǎo)致系統(tǒng)GPS無(wú)數(shù)據(jù)更新窥摄;
*/
public void stopMockLocation() {
if (hasAddTestProvider) {
for (String provider : mockProviders) {
try {
locationManager.removeTestProvider(provider);
} catch (Exception ex) {
// 此處不需要輸出日志镶奉,若未成功addTestProvider,則必然會(huì)出錯(cuò)
// 這里是對(duì)于非正常情況的預(yù)防措施
}
}
hasAddTestProvider = false;
}
}
??注冊(cè)位置服務(wù)崭放,獲取系統(tǒng)位置
// 注冊(cè)位置服務(wù)哨苛,獲取系統(tǒng)位置
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mockLocationManager.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
??最后通過LocationListener.onLocationChanged()回調(diào)方法獲取GPS定位數(shù)據(jù):
private LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(final Location location) {
setLocationData(location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
/**
* 獲取到模擬定位信息,并顯示
*
* @param location 定位信息
*/
private void setLocationData(Location location) {
tvProvider.setText(location.getProvider());
tvTime.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(location.getTime())));
tvLatitude.setText(location.getLatitude() + " °");
tvLongitude.setText(location.getLongitude() + " °");
}
四莹菱、使用模擬定位需先開啟系統(tǒng)設(shè)置中的模擬位置:
-
Android 6.0 以下:【開發(fā)者選項(xiàng) -> 允許模擬位置】
Android 6.0 及以上:【開發(fā)者選項(xiàng) -> 選擇模擬位置信息應(yīng)用】
參考鏈接:
1移国、【科普】GPS、Wifi等各種手機(jī)定位方式的含義及原理詳解
2道伟、Android 使用模擬位置(支持Android 6.0)