Android Tips (10)--allows to simulate the pit under Android m

Source: Internet
Author: User
Tags local time map class

Reprint please indicate the source: a column of Maple Leaf

In this article we will explain the holes allowed to simulate the location under Android M. The students who do the map class should know that in order to avoid the influence of the software simulation position on the normal process we will generally determine whether the user's mobile phone opens the analog location setting, if it is open, the user process is stopped, reminding the user to turn off the analog location settings. There is a simulated location option in the developer options of the Android system, which allows the user to simulate the current location of the device through code, such as a map class application that needs to test the use of the field, by enabling this option to simulate the location by code, Refer to my: Developer options in Android

Allow settings for the impersonation location option in the phone's developer Options settings:

Product Practice:

In our products alone in the car has a pick-up link, through the mobile phone control of the car door, and this time will determine whether the current phone is open to simulate the function of the location, if open, prompt the user and close the function of the analog location. (If the user is allowed to open the analog location function, the malicious user can use the third-party simulation location app to modify the mobile phone positioning information, which will affect the positioning information of our app, when the need for users in the Zhongguancun car, in the ten-miles can be shielded by analog location this operation)

The code to determine if the user opened the impersonation location is as follows:

/** * Determine if the Allow virtual location is turned on, and if it is open, pop the window and let him close it.     Public Static Boolean isallowmocklocation(FinalActivity context) {/** * Determine if the user has opened the analog location function */        BooleanIsOpen = Settings.Secure.getInt (Context.getcontentresolver (), Settings.Secure.ALLOW_MOCK_LOCATION,0) !=0;if(IsOpen) {Config.showtipldialog (context,NULL,"Failed to locate, you need to turn off the" Allow analog location "feature before you can make friends use the car to view nearby vehicles. ","Go to Settings",NewView.onclicklistener () {@Override                 Public void OnClick(View view) {context.startactivity (NewIntent (settings.action_application_development_settings));        }            }); }returnIsOpen; }

In the Open Door page, click the Drivers button to determine if the user has opened the analog position switch, if open prompts the user to close:

When you click the Set button, you will be redirected to the developer options and allow the user to turn off the developer option.

Problems that arise:

But in the Android M model to determine the logic of the problem, some Samsung phones to open the car mode, this time again click on the door, the code will determine the user opened the analog location function, this will block the user's operation, and guide the user to turn off the analog position switch. But the Android m phone has no settings to allow the analog location switch, instead of selecting the analog location information to apply the Settings button.

According to reason, even if the user turned on the car mode this time through the above to determine whether to open the analog location of the code return value should be false (do not open the analog location), but this time used to eject the positioning failure, you need to close the simulation location of the pop-up window, Indicates that the user is returned true by code to determine whether the impersonation location has been opened.

Later, after the investigation, such as allowing the simulation location and other information is stored in a database at the bottom of the system, and our judgment code returns TRUE, the user's underlying allowed impersonation location database value is true.

But this time in Android m because no longer exists to allow the analog location to be replaced by the choice of analog location information application settings, which is equivalent to two sets of the underlying database variable switch, and our code is to allow the simulation location of the database values, in the Android There is no change in M that allows the analog position to be changed, so there is no way to change the value of the allowed analog position under Android M. But isn't Android m using the Select Analog Info app settings? What the hell is this?

There is no development in Android m that allows analog locations, instead: Choose the analog location information app:

The default application under Android M cannot be displayed in the Select analog information application, which requires the following actions:

    • Add Debug-androidmanifest Permissions

After this setup, our application information can be displayed in the simulation location, which is tested when we set the simulation location information for our application, it can only affect the location information of our own application, and cannot affect the location information of other applications. This is also the Android system to solve the simulation of location information bug it.

Bug that allows simulation of location:

Before Android M if we have chosen to allow analog locations for our applications, we can influence the location information of other apps through an app's simulated location operation, which Google believes is incorrect. The purpose of simulating location information is to facilitate the debugging of the app, and it can do some black operations when the operation affects other applications.

For example, through the simulation position, the use of droplets when the simulation of location information grab orders and so on.

So in order to solve this problem, Android M upgraded to allow analog location settings, instead of selecting the simulated location information app settings, by setting this option, only affect the current app, not the location information of other apps.

For example, when we are using some apps to simulate the location of the current phone, this time can not affect the drop location information.

Perform a simulated position operation under Android m:

    • To add permission permissions for the impersonation location in Debug-androidmanifest

    • In the developer options, select your app in the analog location information app

    • Simulating locations by code

 Public  class runnablemocklocation implements Runnable {        @Override         Public void Run() {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 (); }        }    }
    • Get location information by code:
//Position monitoring    PrivateLocationlistener locationlistener=NewLocationlistener () {/** * Trigger when position information changes/*         Public void onlocationchanged(Location location) {Doublelat = Location.getlatitude ();DoubleLot = Location.getlongitude (); String str="Latitude"+lat+"\r\nlongitude:"+lot;            Textview.settext (str); LOG.I (TAG,"Time:"+location.gettime ()); LOG.I (TAG,"Longitude:"+location.getlongitude ()); LOG.I (TAG,"Latitude:"+location.getlatitude ()); LOG.I (TAG,"Altitude:"+location.getaltitude ()); }/** * Triggered when GPS status changes * /         Public void onstatuschanged(String provider,intStatus, Bundle extras) {Switch(status) {when the//gps status is visible                 CaseLOCATIONPROVIDER.AVAILABLE:LOG.I (TAG,"Current GPS status is visible"); Break;when the//gps status is outside the service area                 CaseLOCATIONPROVIDER.OUT_OF_SERVICE:LOG.I (TAG,"Current GPS status is out-of-service status"); Break;//GPS status is paused service                 CaseLOCATIONPROVIDER.TEMPORARILY_UNAVAILABLE:LOG.I (TAG,"Current GPS status is paused service status"); Break; }        }/** * Triggered when GPS is on * /         Public void onproviderenabled(String Provider) {        }/** * Triggered when GPS is disabled */         Public void onproviderdisabled(String Provider) {        }    };
    • See if the location information of other apps is affected

We opened our other applications and found that their location information was not affected. That is to say, the selection of Android m in the analog location information is applied with Android m below the allowed analog location difference.

The difference between allowing the simulated location and selecting the simulated location information:

    • Allows analog locations to influence positioning information for other applications by simulating location

    • Selecting the analog location information app can only affect positioning information for the current app

    • Allows simulated location and selection of simulated location information The application is eventually saved in two database tables in the system and does not affect each other

And our cell phone is judged to allow the analog position switch, in Android m if the decision to open the switch, but the system has turned off the setting operation of the switch, so we only need to block this value.

The Final Solution:

/** * Determine if the Allow virtual location is turned on, and if it is open, pop the window and let him close it.     Public Static Boolean isallowmocklocation(FinalActivity context) {BooleanIsOpen = Settings.Secure.getInt (Context.getcontentresolver (), Settings.Secure.ALLOW_MOCK_LOCATION,0) !=0;/** * This judgment API is ANDROIDM the following API, because Android M has not been closed to allow the analog location of the entrance, so once detected to open the analog location, and is more than Android m, the default setting is not open Analog position * /        if(IsOpen && Build.VERSION.SDK_INT > A) {IsOpen =false; }if(IsOpen) {Config.showtipldialog (context,NULL,"Failed to locate, you need to turn off the" Allow analog location "feature before you can make friends use the car to view nearby vehicles. ","Go to Settings",NewView.onclicklistener () {@Override                 Public void OnClick(View view) {context.startactivity (NewIntent (settings.action_application_development_settings));        }            }); }returnIsOpen; }

That is, when we determine that the current device is open to allow analog location, in judging the version of the mobile phone system, if it is Android m and above, it is blocked regardless. Perhaps some students will ask that Android M on the choice of analog location information application has influence? The answer is no, because our app does not add permissions to allow impersonation locations, so it does not appear at all in selecting a simulated Location app list, which will not perform the operation of the simulated location.

So the final solution is to detect whether the device has turned on the analog location option, if it is turned on, then determine whether the current device is Android M that is above, if so, blocking the user operation, or to guide the user to turn off the analog location option.

Android Tips (10)--Allow to simulate pits under Android m

Related Article

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.