簡體   English   中英

Android-IntelliJ模擬器問題中的模擬位置

[英]Android - Mock Locations in IntelliJ Emulator Issues

我對Android開發相當陌生,而我正在嘗試開發的應用程序正遇到一個奇怪的問題,以使其與時俱進。 有時,此應用似乎可以正常運行,但大多數情況下卻無法正常運行。 我正在嘗試通過應用簡單地設置設備的GPS緯度/經度; 僅此而已。 我利用了從其他帖子中收集到的很多知識,但是最后,我不確定我的應用是否由於模擬器的怪癖而無法正常工作,或者是我的代碼。 作為參考,我正在使用IntelliJ,目標是2.3.3,確實在模擬器上啟用了GPS,並且確實在清單中設置了適當的權限。

奇怪的是,當我打開仿真器,然后通過終端telnet並通過“ geo fix”設置地理位置后,再回到應用程序中時,它有時可以正常工作,它顯示了我輸入的GPS經緯度。 但是通常它根本不起作用。 當我嘗試通過代碼中的模擬位置以編程方式進行操作時,會得到相同的結果。

這是我的onCreate()方法頂部的代碼(locationManager是一個類級別的字段):

    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    locationManager.addTestProvider(LocationManager.GPS_PROVIDER, false, false, false, false, false, true, true, 0, 5);

..並且當用戶在用戶界面中輸入緯度/經度並點擊提交按鈕時,在提取字段並完成一些輕驗證后,將調用以下代碼:

Location mLocation = new Location(LocationManager.GPS_PROVIDER);
mLocation.setLatitude(Float.valueOf(inputLat));
mLocation.setLongitude(Float.valueOf(inputLong));
mLocation.setTime(System.currentTimeMillis());

locationManager.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);
locationManager.setTestProviderLocation(LocationManager.GPS_PROVIDER, mLocation);

refreshTheLocation();

供參考,refreshTheLocation()方法是:

public void refreshTheLocation() {
    LocationResult locationResult = new LocationResult(){
        @Override
        public void gotLocation(Location location){
            final Location finalLoc = location;

            if (location != null) {
                runOnUiThread(new Runnable() {
                    public void run() {
                        TextView gpsTextView = (TextView) findViewById(R.id.gps_label);
                        gpsTextView.setText("GPS DATA FOUND:\nLatitude: " + finalLoc.getLatitude() + "\nLongitude: " + finalLoc.getLongitude());
                     }
                });
            } else {
                runOnUiThread(new Runnable() {
                   public void run() {
                      TextView gpsTextView = (TextView) findViewById(R.id.gps_label);
                      gpsTextView.setText("GPS data not found");
                   }
                });
            }
        }
   };
   MyLocation myLocation = new MyLocation();
   myLocation.getLocation(this, locationResult);
}  

“ MyLocation”類只是該帖子評分最高的答案的復制/粘貼:

在Android上獲取用戶當前位置的最簡單,最可靠的方法是什么?

關於我的問題可能有什么想法嗎? 似乎有1/5的時間可以獲取GPS坐標並顯示出來,而其他4/5的時間則沒有。 當它起作用時,當我通過輸入字段和按鈕更改坐標時,它永遠不會更新坐標。

請對我好一點,因為我對這些東西還很陌生。 ;)預先感謝您提供的任何幫助!

如果您以2.3.3版為目標並且已經在2.3.3版AVD上進行過測試,建議您切換到API的2.3.3 Google API版本,然后嘗試模擬位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM