尝试获取当前的经度和纬度。
CODE:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new myLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, locationListener);
}
class myLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
tvLang.setText("");
Toast.makeText(getBaseContext(), "Location changed: Lat: " + loc.getLatitude() + " Lng: " + loc.getLongitude(), Toast.LENGTH_SHORT)
.show();
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
但是当我在LG G2上运行时 - 它永远不会进入" onLocationChanged"功能。 GPS已开启,我可以在谷歌地图中找到自己。 试图重启手机几次。
我做错了什么?