Android studio uses mocklocation for virtual location

First, you need to add the "get simulation location information" permission in the androidmanifest.xml file.

<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"/>

Then you need to open the developer permission of Android studio simulator

Step1: find the setting of Android and click on it

Step 2: pull to the bottom, click system, and then pull to the bottom to find the mobile phone. Click the mobile phone version 7 times in a row, and then return to the previous layer to have a developer choose

Step3: click developer options, pull down, find the select mock location app, and click the app you need to simulate location.

I posted the specific app code directly

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,false,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;
 }
}

summary

The above is what Xiaobian introduced to you. Android studio uses mocklocation virtual positioning. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support to our website! If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>