這篇文章主要介紹androidstudio如何使用Mocklocation虛擬定位,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)基于成都重慶香港及美國(guó)等地區(qū)分布式IDC機(jī)房數(shù)據(jù)中心構(gòu)建的電信大帶寬,聯(lián)通大帶寬,移動(dòng)大帶寬,多線BGP大帶寬租用,是為眾多客戶提供專業(yè)服務(wù)器托管報(bào)價(jià),主機(jī)托管價(jià)格性價(jià)比高,為金融證券行業(yè)西部信息機(jī)房,ai人工智能服務(wù)器托管提供bgp線路100M獨(dú)享,G口帶寬及機(jī)柜租用的專業(yè)成都idc公司。首先需要在 AndroidManifest.xml 文件中添加「獲取模擬定位信息」權(quán)限。
然后需要打開android studio模擬器的開發(fā)者權(quán)限
step1:找到安卓的setting點(diǎn)開
step2:拉到最底部,點(diǎn)擊System,再拉到最底部找到關(guān)于手機(jī),手機(jī)版本連續(xù)點(diǎn)擊7次,返回上一層就有一個(gè)開發(fā)者選擇
step3:點(diǎn)擊Developer options,往下拉,找到select mock location app,點(diǎn)擊你需要模擬定位的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;//點(diǎn)擊按鈕訪問 @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); // 經(jīng)度(度) mockLocation.setAltitude(30); // 高程(米) mockLocation.setBearing(180); // 方向(度) mockLocation.setSpeed(10); //速度(米/秒) mockLocation.setAccuracy(0.1f); // 精度(米) mockLocation.setTime(10); // 本地時(shí)間 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) { } }; //獲取到位置管理器實(shí)例 mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); //獲取到GPS_PROVIDER final Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); //偵聽位置發(fā)生變化,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) { // 當(dāng)GPS Location Provider可用時(shí),更新位置 updata(mLocationManager.getLastKnownLocation(provider)); } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Override public void onLocationChanged(Location location) { // 當(dāng)GPS定位信息發(fā)生改變時(shí),更新位置 String temp = updata(location); //postinfotoweb(temp); } }); //創(chuàng)建發(fā)送http請(qǐng)求的按鈕 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) {//監(jiān)聽按鈕 new Thread(new Runnable() {//創(chuàng)建子線程 @Override public void run() { //getwebinfo();//把路徑選到MainActivity中 String temp = updata(location); System.out.println("location" + temp); } }).start();//啟動(dòng)子線程 } }); //更新位置信息顯示到TextView String temp = updata(location); //postinfotoweb(temp); } private String updata(Location location){ if(location != null){ StringBuilder sb = new StringBuilder(); sb.append("實(shí)時(shí)的位置信息:\n"); sb.append("經(jīng)度:"); 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當(dāng)?shù)貢r(shí)間:"); sb.append(location.getTime()); return sb.toString(); } return null; }}
以上是“androidstudio如何使用Mocklocation虛擬定位”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!