</RelativeLayout>
-------------------代码-------------------------
public class MainActivity extends Activity implements OnClickListener {
/**显示经纬度的TextView*/
private TextView localTv = null;
/**操作Button*/
private Button getBtn = null;
/**定位管理器*/
private LocationManager mLocationManager;
/**经度*/
private double latitude = 0;
/**纬度*/
private double longitude = 0;
@SuppressLint("HandlerLeak")
private Handler localHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
double[] mess = (double[]) msg.obj;
localTv.setText("您所在位置经度为:" + mess[0] + "\t您所在位置纬度为:" + mess[1]);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
localTv = (TextView) findViewById(R.id.show_tv);
getBtn = (Button) findViewById(R.id.get_btn);
getBtn.setOnClickListener(this);
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
public void getLocalInfo() {
new Thread() {
@Override
public void run() {
Location location = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
/**经度*/
latitude = location.getLatitude();
/**纬度*/
longitude = location.getLongitude();
/**谢谢到message中*/
double[] data = { latitude, longitude };
Message msg = localHandler.obtainMessage();
msg.obj = data;
localHandler.sendMessage(msg);
}
}
}.start();
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.get_btn) {
getLocalInfo();
}
}
}
原文:http://blog.csdn.net/true100/article/details/45152535