This article for everyone to share the Android simulation location of the use, support Android6.0, for your reference, the specific content as follows
1, open the system setup in the simulation position
Android 6.0 below : "Developer option-> allow analog Locations"
Android 6.0 and above : "Developer option-> Select analog location information Application"
Androidmanifest.xml
<uses-permission android:name= "Android.permission.ACCESS_MOCK_LOCATION"/>
<uses-permission android: Name= "Android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name= " Android.permission.ACCESS_COARSE_LOCATION "/>
2, to determine whether to open the system simulation position
Android 6.0 below : Use Settings.Secure.ALLOW_MOCK_LOCATION to judge.
Android 6.0: Open "Allow analog locations"
Boolean canmockposition = Settings.Secure.getInt (Getcontentresolver (), Settings.Secure.ALLOW_MOCK_LOCATION, 0)!= 0
Android 6.0 and above : There is no "Allow analog location" option, while discarding the Settings.Secure.ALLOW_MOCK_LOCATION, can not be judged by the above method. The method of "Selecting simulation location information application" is added, and it is necessary to choose the application of simulation location. But do not know how to obtain the current selection of applications, so by whether the Addtestprovider method can be successfully executed to determine, if the current application is not selected, then Addtestprovider throws an exception.
Boolean hasaddtestprovider = false;
Boolean canmockposition = (Settings.Secure.getInt (Getcontentresolver (), Settings.Secure.ALLOW_MOCK_LOCATION, 0)!= 0) ||
Build.VERSION.SDK_INT > 22; if (canmockposition && Hasaddtestprovider = = False) {try {String providerstr = Locationmanager.gps_provide
R
Locationprovider Provider = Locationmanager.getprovider (PROVIDERSTR); if (provider!= null) {Locationmanager.addtestprovider (Provider.getname (), PROVIDER.REQUIRESN Etwork (), 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, tr UE, Criteria.power_high, Criteria.accuracy_fine);
} locationmanager.settestproviderenabled (Providerstr, true);
Locationmanager.settestproviderstatus (PROVIDERSTR, locationprovider.available, NULL, System.currenttimemillis ());
The impersonation location can be hasaddtestprovider = true;
Canmockposition = true;
catch (SecurityException e) {canmockposition = false;
}
}
3. Simulation location
Private class Runnablemocklocation implements Runnable {@Override public void run () {while (true) {try
{Thread.Sleep (intervel);
if (hasaddtestprovider () = = false) {continue;
The try {//Impersonation position (under the premise of Addtestprovider success) String providerstr = Locationmanager.gps_provider;
Location mocklocation = new Location (PROVIDERSTR); Mocklocation.setlatitude (22); Dimension (degree) Mocklocation.setlongitude (113); Longitude (degree) mocklocation.setaltitude (30); Elevation (m) mocklocation.setbearing (180); Direction (degree) mocklocation.setspeed (10); Speed (M/s) mocklocation.setaccuracy (0.1f); Precision (m) mocklocation.settime (New Date (). GetTime ()); local time if (Build.VERSION.SDK_INT >= build.version_codes.
JELLY_BEAN_MR1) {Mocklocation.setelapsedrealtimenanos (Systemclock.elapsedrealtimenanos ()); } Locationmanager.settestproviderlocatiOn (Providerstr, mocklocation);
catch (Exception e) {//Prevent users from turning off analog locations or selecting other applications Stopmocklocation () during software operation;
} catch (Interruptedexception e) {e.printstacktrace ();
catch (Exception e) {e.printstacktrace ();
}
}
}
}
4, stop the simulation position
/**
* Stop the impersonation location, so that you cannot restore the use of the system location when the analog data is turned on
* If the simulation position is not turned on, the Removetestprovider will throw an exception;
* If you have addtestprovider, turn off the simulation position, Removetestprovider will cause system GPS no data update;
/public
void Stopmocklocation () {
if (hasaddtestprovider) {
try {
Locationmanager.removetestprovider (locationmanager.gps_provider);
} catch (Exception ex) {
//If the Addtestprovider is not successful, or if the system emulation location is turned off, an error is necessary
}
Hasaddtestprovider = false;
}
Android Studio Project
This is the entire content of this article, I hope to learn more about Android software programming help.