簡體   English   中英

在Android模擬器上設置GPS位置

[英]Setting GPS position on android emulator

我正在嘗試使用telnet並使用DDMS透視圖在模擬器上設置GPS位置。 似乎沒有任何效果,因為如果您認為我的代碼在屏幕上顯示為“無法獲取GPS位置”。 當我在手機上運行時,該應用程序可以正常工作。 碼:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tvLat = (TextView) findViewById(R.id.tvLat);
    tvLongi = (TextView) findViewById(R.id.tvLongi);
    tvLat.setText("test");
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria crit = new Criteria();
    providers = lm.getBestProvider(crit, false);
    Location loc = lm.getLastKnownLocation(providers);
    if (loc != null) {
        x =  loc.getLatitude();
        y =  loc.getLongitude();

        t = "Current latitude: " + x;
        t1 = "Current longitude: " + y;
        tvLat.setText(t);
        tvLongi.setText(t1);
    } else {
        tvLat.setText("Couldn't get a GPS location");
    }

}

@Override
public void onLocationChanged(Location loc) {
    // TODO Auto-generated method stub
    x = loc.getLatitude();
    y = loc.getLongitude();
    t = "Current latitude: " + x;
    t1 = "Current longitude: " + y;

    tvLat.setText(t);
    tvLongi.setText(t1);

}

@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub
    tvLat.setText("GPS disabled");
}

@Override
public void onProviderEnabled(String arg0) {

    tvLat.setText("GPS enabled");
}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {


}
Permissions:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission>

從位於Eclipse左下角的Emulator Control選項卡的DDMS Perspective ,您可以看到Locations Controls 從那里您可以輸入經度和緯度,然后單擊發送以將這些坐標發送到選定的模擬器。

也許你仍然無法獲得最后知道的位置,但這將觸發onLocationChanged()方法。

編輯:我看到您的類實現了LocationListener 您是否使用lm.requestLocationUpdates()注冊了您的課程以更改位置?

編輯2:我的意思是:

lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,this);
lm.requestLocationUpdates(LocationManager.GPS, 0, 0,this);

暫無
暫無

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

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