Android uses analog location (Android 6.0 supported)

Source: Internet
Author: User

Turn on simulation location in System setup
    • Android 6.0 below: "Developer options, allow simulation location"

    • Android 6.0 and above: "Developer options, select analog location information app"

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"/>
Determine if the system simulation location is turned on
  • Android 6.0 below: Use Settings.Secure.ALLOW_MOCK_LOCATION judgment.

    6.0 以下:是否开启【允许模拟位置】boolean canMockPosition = Settings.Secure.getInt(getContentResolver(), Settings.Secure.ALLOW00
  • Android 6.0 and above: There is no "Allow impersonation location" option and is deprecated Settings.Secure.ALLOW_MOCK_LOCATION and cannot be judged by the method above. Added the option to "choose the analog location information application", which requires selecting an application that uses a simulated location. However, it is not known how to get the currently selected application, so the Addtestprovider throws an exception if the current application is not selected by the ability to successfully execute the Addtestprovider method. (Does anyone know that there is no one to know ...) )

    Boolean Hasaddtestprovider = False;Boolean canmockposition = (Settings. Secure. GetInt(Getcontentresolver (), Settings. Secure. allow_mock_location,0) !=0)        || Build. VERSION. SDK_int > A;if (canmockposition && Hasaddtestprovider = = False) {try {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, Crit Eria. POWER_high, Criteria. Accuracy_fine);} Locationmanager. settestproviderenabled(Providerstr, True);Locationmanager. Settestproviderstatus(Providerstr, Locationprovider. AVAILABLE, NULL, System. Currenttimemillis());Analog location Available Hasaddtestprovider = True;Canmockposition = True;} catch (SecurityException e) {canmockposition = False;}}
Analog location
Private  class runnablemocklocation implements Runnable {    @Override     Public void Run() { while(true) {Try{Thread.Sleep (intervel);if(Hasaddtestprovider () = =false) {Continue; }Try{//Analog location (Addtestprovider success)String providerstr = Locationmanager.gps_provider; Location mocklocation =NewLocation (PROVIDERSTR); Mocklocation.setlatitude ( A);//Dimension (degrees)Mocklocation.setlongitude (113);//Longitude (degrees)Mocklocation.setaltitude ( -);//elevation (m)Mocklocation.setbearing ( the);//direction (degrees)Mocklocation.setspeed (Ten);//Speed (M/s)Mocklocation.setaccuracy (0.1f);//accuracy (m)Mocklocation.settime (NewDate (). 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 shutting down the analog location or selecting another application during software operationStopmocklocation (); }            }Catch(Interruptedexception e)            {E.printstacktrace (); }Catch(Exception e)            {E.printstacktrace (); }        }    }}
Stop the simulation position
/** * 停止模拟位置,以免启用模拟数据后无法还原使用系统位置 * 若模拟位置未开启,则removeTestProvider将会抛出异常; * 若已addTestProvider后,关闭模拟位置,未removeTestProvider将导致系统GPS无数据更新; */publicvoidstopMockLocation() {    if (hasAddTestProvider) {        try {            locationManager.removeTestProvider(LocationManager.GPS_PROVIDER);        catch (Exception ex) {            // 若未成功addTestProvider,或者系统模拟位置已关闭则必然会出错        }        false;    }}
Android Studio Project

Android Analog Location

Android uses analog location (Android 6.0 supported)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.