新建项目:
1 New Android Project->
2 Project name:Location
3 Build Target:Android 2.2
4 Application name: AppWidget
5 Package name:com.b510
6 Create Activity:MainActivity
7 Min SDK Version:9
8 Finish
项目结构:
运行效果:
代码部分:
AndroidManifest.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.b510"
4 android:versionCode="1"
5 android:versionName="1.0">
6 <uses-sdk android:minSdkVersion="9" />
7
8 <application android:icon="@drawable/icon" android:label="@string/app_name">
9 <activity android:name=".MainActivity"
10 android:label="@string/app_name">
11 <intent-filter>
12 <action android:name="android.intent.action.MAIN" />
13 <category android:name="android.intent.category.LAUNCHER" />
14 </intent-filter>
15 </activity>
16
17 </application>
18 <!-- from android api:Allows an application to access fine (e.g., GPS) location -->
19 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
20 </manifest>
main.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 >
7 <!-- 表示经度 -->
8 <TextView
9 android:id="@+id/longitutde"
10 android:layout_width="fill_parent"
11 android:layout_height="wrap_content"
12 />
13 <!-- 表示纬度 -->
14 <TextView
15 android:id="@+id/latitude"
16 android:layout_width="fill_parent"
17 android:layout_height="wrap_content"
18 />
19 </LinearLayout>
MainActivity.java
1 package com.b510;
2
3 import android.app.Activity;
4 import android.content.Context;
5 import android.location.Location;
6 import android.location.LocationManager;
7 import android.os.Bundle;
8 import android.widget.TextView;
9
10 public class MainActivity extends Activity {
11 /** 显示经度 */
12 private TextView longitude;
13 /** 显示维度 */
14 private TextView latitude;
15
16 /** Called when the activity is first created. */
17 @Override
18 public void onCreate(Bundle savedInstanceState) {
19 super.onCreate(savedInstanceState);
20 setContentView(R.layout.main);
21
22 //从xml中找到已经定义好的TextView:longitude,latitude
23 longitude = (TextView) findViewById(R.id.longitutde);
24 latitude = (TextView) findViewById(R.id.latitude);
25 //初始化一个location
26 Location location = getLocation(this);
27 //设置TextView控件的显示信息:经度和维度
28 longitude.setText("经度:" + location.getLongitude());
29 latitude.setText("维度:" + location.getLatitude());
30 }
31 @SuppressWarnings("static-access")
32 private Location getLocation(Context context) {
33 //You do not instantiate this class directly;
34 //instead, retrieve it through:
35 //Context.getSystemService(Context.LOCATION_SERVICE).
36 LocationManager locationManager = (LocationManager) context
37 .getSystemService(context.LOCATION_SERVICE);
38 //获取GPS支持
39 Location location = locationManager
40 .getLastKnownLocation(locationManager.GPS_PROVIDER);
41 if (location == null) {
42 //获取NETWORK支持
43 location = locationManager
44 .getLastKnownLocation(locationManager.NETWORK_PROVIDER);
45 }
46 return location;
47 }
48 }