[英]No location fix on GPS - Android
我從此鏈接復制粘貼了以下代碼。 當我使用活動設備而不是模擬器時,未顯示任何位置信息。 甚至啟用了Gps
public class UseGps extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate (Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
setContentView (R.layout.main);
/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager) getSystemService (Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener ();
mlocManager.requestLocationUpdates (LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged (Location loc)
{
loc.getLatitude ();
loc.getLongitude ();
String Text = "My current location is: " + "Latitud = " + loc.getLatitude () + "Longitud = " + loc.getLongitude ();
Toast.makeText (getApplicationContext (), Text, Toast.LENGTH_SHORT).show ();
}
@Override
public void onProviderDisabled (String provider)
{
Toast.makeText (getApplicationContext (), "Gps Disabled", Toast.LENGTH_SHORT).show ();
}
@Override
public void onProviderEnabled (String provider)
{
Toast.makeText (getApplicationContext (), "Gps Enabled", Toast.LENGTH_SHORT).show ();
}
@Override
public void onStatusChanged (String provider, int status, Bundle extras)
{
}
}/* End of Class MyLocationListener */
}/* End of UseGps Activity */
看起來很正確。 如果您沒有忘記添加權限,也許您的設備找不到衛星。 只需嘗試在建築物外進行測試即可。 或者只是嘗試使用
編輯:
mlocManager.requestLocationUpdates(mlocManager.getBestProvider(new Criteria(), true), 0, 0, mlocListener);
而不是LocationManager.GPS_PROVIDER。
看起來還可以。 也許您的設備確實找不到衛星。 嘗試使用偽造的GPS應用程序(例如https://play.google.com/store/apps/details?id=com.lexa.fakegps&hl=ru)運行此代碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.