我复制粘贴了此link中的以下代码。当我使用有源设备而不是模拟器时,没有显示位置信息。甚至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 */
答案 0 :(得分:0)
看起来很正确。如果您没有忘记添加权限,可能是您的设备无法找到卫星。试着在建筑物外测试它。 或者只是尝试使用
修改强>
mlocManager.requestLocationUpdates(mlocManager.getBestProvider(new Criteria(), true), 0, 0, mlocListener);
而不是LocationManager.GPS_PROVIDER。
答案 1 :(得分:0)
看起来不错。 也许你的设备真的找不到卫星。尝试使用假的gps应用程序运行此代码,例如https://play.google.com/store/apps/details?id=com.lexa.fakegps&hl=ru