In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces androidstudio how to use Mocklocation virtual positioning, the article is very detailed, has a certain reference value, interested friends must read it!
First of all, you need to add the permission to "get simulation location information" in the AndroidManifest.xml file.
Then you need to open the developer permissions of the android studio simulator.
Step1: find Android setting and click on it.
Step2: pull to the bottom, click System, and then pull to the bottom to find out about the phone. The phone version has been clicked seven times in a row, and a developer will choose when you return to the upper floor.
Step3: click Developer options, drop down, find select mock location app, and click on the app you want to simulate.
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 {TextView private LocationManager mLocationManager used by private TextView tv;// to display information / / location Manager private Button btn;// Click the button to visit @ 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, Criteria.POWER_HIGH, Criteria.ACCURACY_FINE) } locationManager.setTestProviderEnabled (providerStr, true); locationManager.setTestProviderStatus (providerStr, LocationProvider.AVAILABLE, null, System.currentTimeMillis ()); / / Simulation position available 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); / / Dimension (degree) mockLocation.setLongitude (113); / / Longitude (degree) mockLocation.setAltitude (30) / / elevation (m) mockLocation.setBearing (180); / / Direction (degree) mockLocation.setSpeed (10); / Speed (m / s) mockLocation.setAccuracy (0.1f); / / Precision (m) mockLocation.setTime (10); / / Local time 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) {}} / / get the location manager instance mLocationManager = (LocationManager) getSystemService (Context.LOCATION_SERVICE); / / get GPS_PROVIDER final Location location = mLocationManager.getLastKnownLocation (LocationManager.GPS_PROVIDER) / / the listening location has changed. Update once in 2000 milliseconds and once over 8 meters. Update 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) {/ / update location updata (mLocationManager.getLastKnownLocation (provider) when GPS Location Provider is available) } @ Override public void onProviderDisabled (String provider) {/ / TODO Auto-generated method stub} @ Override public void onLocationChanged (Location location) {/ / when the GPS location information changes, update the location String temp = updata (location); / / postinfotoweb (temp);}}); / / create the button super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); btn = findViewById (R.id.main_btn) to send the http request / bind ID btn.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View view) {/ / listen button new Thread (new Runnable () {/ / create child thread @ Override public void run () {/ / getwebinfo (); / / select the path to MainActivity String temp = updata (location); System.out.println ("location" + temp);}}) .start (); / / start child thread}}) / / Update location information to TextView String temp = updata (location); / / postinfotoweb (temp);} private String updata (Location location) {if (location! = null) {StringBuilder sb = new StringBuilder (); sb.append ("Real-time location information:\ n"); sb.append ("longitude:"); sb.append (location.getLongitude ()); sb.append ("\ nLatitude:"); sb.append (location.getLatitude ()); sb.append ("\ n height:") Sb.append (location.getAltitude ()); sb.append ("\ nSpeed:"); sb.append (location.getSpeed ()); sb.append ("\ nDirection:"); sb.append (location.getBearing ()); sb.append ("\ nLocal time:"); sb.append (location.getTime ()); return sb.toString ();} return null;}}
The above is all the contents of the article "how androidstudio uses Mocklocation Virtual location". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 264
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.