简体   繁体   中英

How to get mock locations to work in android studio for phones that only have “Select mock location app” option in Developer Options?

In Developer Options, the newer versions of Android at some point went from "Turn on Mock Locations" to "Select mock location app" option. The problem is that I want to be able to send it mock locations inside android studio in "Location" under "Extended Controls" where you can set Longitude and Latitude and even send a GPS data playback.

But there is no "Turn on Mock Locations" option for these newer android versions. They all only have one option "Select mock location app". But I don't want to use a mock location app installed on the emulator phone. I want to send it mock locations inside the android studio itself. How do I achieve this?

  1. Add entry to your manifest / debug manifest for testing:

    \nuses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" \n
  2. Run your app on your device / emulator to get it installed.

It will fail but now it will also be listed in 'Select mock location app'. Select it and then you should be able to do whatever you need.

Edit: If you are running InstrumentationTest make sure you have added your app Location permission in the Application Settings because since API 22 it requires user consent AND manifest entry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM