public class MainActivity extends Activity {
private LocationManager lm;
private MyLocationListener listener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
listener = new MyLocationListener();
lm.requestLocationUpdates("gps", 0, 0, listener);
}
public class MyLocationListener implements LocationListener{
//当位置发生变化的时候
@Override
public void onLocationChanged(Location location) {
String longitude = "经度:"+location.getLongitude()+"\n";
String latitude = "纬度:"+location.getLatitude()+"\n";
String accuracy = "精确度:"+location.getAccuracy()+"\n";
TextView textView = new TextView(MainActivity.this);
textView.setText(longitude+latitude+accuracy);
setContentView(textView);
}
//某个位置提供者的状态发生变化的时候 打开--》关闭 ;关闭--》开启
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
//某个位置提供者可用
@Override
public void onProviderEnabled(String provider) {
}
//某个位置提供者不可用
@Override
public void onProviderDisabled(String provider) {
}
}
@Override
protected void onDestroy() {
super.onDestroy();
lm.removeUpdates(listener);
listener = null;
}
}
android GPS定位代码
最新推荐文章于 2021-05-26 01:50:23 发布