欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android studio 使用Mocklocation虚拟定位

程序员文章站 2022-03-31 14:08:14
首先需要在 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点开

android studio 使用Mocklocation虚拟定位

step2:拉到最底部,点击system,再拉到最底部找到关于手机,手机版本连续点击7次,返回上一层就有一个开发者选择

android studio 使用Mocklocation虚拟定位

step3:点击developer options,往下拉,找到select mock location app,点击你需要模拟定位的app就行了。

android studio 使用Mocklocation虚拟定位

具体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虚拟定位,希望对大家有所帮助