android studio 使用Mocklocation虚拟定位
程序员文章站
2022-07-05 19:30:31
首先需要在 androidmanifest.xml 文件中添加「获取模拟定位信息」权限。
首先需要在 androidmanifest.xml 文件中添加「获取模拟定位信息」权限。
<uses-permission android:name="android.permission.access_fine_location"/> <uses-permission android:name="android.permission.internet"/> <uses-permission android:name="android.permission.access_coarse_location"/> <uses-permission android:name="android.permission.access_mock_location" tools:ignore="mocklocation"/> <uses-permission android:name="android.permission.read_external_storage"/> <uses-permission android:name="android.permission.write_external_storage"/> <uses-permission android:name="android.permission.write_external_storag"/>
然后需要打开android studio模拟器的开发者权限
step1:找到安卓的setting点开
step2:拉到最底部,点击system,再拉到最底部找到关于手机,手机版本连续点击7次,返回上一层就有一个开发者选择
step3:点击developer options,往下拉,找到select mock location app,点击你需要模拟定位的app就行了。
具体app的代码,我直接贴出来了
import android.location.criteria; import android.location.locationprovider; import android.os.build; import android.os.bundle; import android.os.systemclock; import android.provider.settings; import android.widget.textview; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.app.activity; import android.content.context; import android.view.view; import android.widget.button; import java.util.date; public class mainactivity extends activity { private textview tv;//用于显示信息的textview private locationmanager mlocationmanager;//位置管理器 private button btn;//点击按钮访问 @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); boolean hasaddtestprovider = false; locationmanager locationmanager = (locationmanager) getsystemservice(context.location_service); 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_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 , criteria.power_high, 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 (hasaddtestprovider == true) { string providerstr = locationmanager.gps_provider; location mocklocation = new location(providerstr); mocklocation.setlatitude(22); // 维度(度) mocklocation.setlongitude(113); // 经度(度) mocklocation.setaltitude(30); // 高程(米) mocklocation.setbearing(180); // 方向(度) mocklocation.setspeed(10); //速度(米/秒) mocklocation.setaccuracy(0.1f); // 精度(米) mocklocation.settime(10); // 本地时间 if (build.version.sdk_int >= build.version_codes.jelly_bean_mr1) { mocklocation.setelapsedrealtimenanos(systemclock.elapsedrealtimenanos()); } locationmanager.settestproviderlocation(providerstr, mocklocation); } else { system.out.println("hasaddtestprovider" + hasaddtestprovider); } locationmanager locmgr = (locationmanager) getsystemservice(location_service); locationlistener lis = new locationlistener() { public void onlocationchanged(location location) { //you will get the mock location } @override public void onstatuschanged(string s, int i, bundle bundle) { } @override public void onproviderenabled(string s) { } @override public void onproviderdisabled(string s) { } }; //获取到位置管理器实例 mlocationmanager = (locationmanager) getsystemservice(context.location_service); //获取到gps_provider final location location = mlocationmanager.getlastknownlocation(locationmanager.gps_provider); //侦听位置发生变化,2000毫秒更新一次,位置超过8米也更新一次 mlocationmanager.requestlocationupdates(locationmanager.gps_provider, 2000, 8, new locationlistener() { @override public void onstatuschanged(string provider, int status, bundle extras) { // todo auto-generated method stub } @override public void onproviderenabled(string provider) { // 当gps location provider可用时,更新位置 updata(mlocationmanager.getlastknownlocation(provider)); } @override public void onproviderdisabled(string provider) { // todo auto-generated method stub } @override public void onlocationchanged(location location) { // 当gps定位信息发生改变时,更新位置 string temp = updata(location); //postinfotoweb(temp); } }); //创建发送http请求的按钮 super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); btn = findviewbyid(r.id.main_btn);//绑定id btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) {//监听按钮 new thread(new runnable() {//创建子线程 @override public void run() { //getwebinfo();//把路径选到mainactivity中 string temp = updata(location); system.out.println("location" + temp); } }).start();//启动子线程 } }); //更新位置信息显示到textview string temp = updata(location); //postinfotoweb(temp); } private string updata(location location){ if(location != null){ stringbuilder sb = new stringbuilder(); sb.append("实时的位置信息:\n"); sb.append("经度:"); sb.append(location.getlongitude()); sb.append("\n纬度:"); sb.append(location.getlatitude()); sb.append("\n高度:"); sb.append(location.getaltitude()); sb.append("\n速度:"); sb.append(location.getspeed()); sb.append("\n方向:"); sb.append(location.getbearing()); sb.append("\n当地时间:"); sb.append(location.gettime()); return sb.tostring(); } return null; } }
总结
以上所述是小编给大家介绍的android studio 使用mocklocation虚拟定位,希望对大家有所帮助
上一篇: vue js秒转天数小时分钟秒的实例代码
推荐阅读
-
详解如何使用Android Studio开发Gradle插件
-
android studio 使用adb 命令传递文件到android 设备的方法
-
Android studio怎么使用代码检查工具Analyze?
-
android studio怎么使用Build工具进行签名打包?
-
Android studio使用git代码追溯到上个文件的教程?
-
Android studio怎么创建Android虚拟机?
-
Android Studio虚拟机启动的解决办法
-
Android Studio finish()方法的使用与解决app点击“返回”(直接退出)
-
Android studio怎么使用git获取最新内容然后合并?
-
详解Android Studio3.5及使用AndroidX的一些坑