android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
android:id="@+id/show_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="@string/hello_world" /> android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:id="@+id/get_btn" android:text="我要經(jīng)緯度" /> -------------------代碼------------------------- public class MainActivity extends Activity implements OnClickListener { /**顯示經(jīng)緯度的TextView*/ private TextView localTv = null; /**操作Button*/ private Button getBtn = null; /**定位管理器*/ private LocationManager mLocationManager; /**經(jīng)度*/ 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("您所在位置經(jīng)度為:" + 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) { /**經(jīng)度*/ 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(); } } }