User Location能做什么?
AndroidManifest,xml:
MainActivity:
1.获取用户的位置
2.追踪用户的移动
User Location的关键API
1.Location Manager:用于管理Android的用户定位服务;
2.Location Providers:提供多种定位方式供开发者选择;
定位方式的分类
1.GPS定位:
使用GPS卫星进行定位,需要在
AndroidMannifest.xml当中声明如下权限:
android.permission.ACCESS_FINE_LOCATION
2.NETWORK定位:
使用信号接收塔和Wi-Fi介入点进行定位,需要在
AndroidManifest.xml当中声明如下权限:
android.permission.ACCESS_FINE_LOCATION
或
android.permission.ACCESS_COARSE_LOCATION
获取用户的当前位置
1.在AndroidManifest.xml当中声明相应的权限;
2.获取LocationManager对象;
3.选择LocationProvider;
4.绑定LocationListener对象;
Main.xml:
strings.xml:
Main.xml:
1
<?
xml version="1.0" encoding="utf-8"
?>
2 < LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
3 android:layout_width ="fill_parent"
4 android:layout_height ="fill_parent"
5 android:orientation ="vertical" >
6
7 < TextView
8 android:layout_width ="fill_parent"
9 android:layout_height ="wrap_content"
10 android:text ="@string/hello" />
11
12 < Button
13 android:id ="@+id/locationButtonId"
14 android:layout_width ="fill_parent"
15 android:layout_height ="wrap_content"
16 android:text ="@string/location" />
2 < LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
3 android:layout_width ="fill_parent"
4 android:layout_height ="fill_parent"
5 android:orientation ="vertical" >
6
7 < TextView
8 android:layout_width ="fill_parent"
9 android:layout_height ="wrap_content"
10 android:text ="@string/hello" />
11
12 < Button
13 android:id ="@+id/locationButtonId"
14 android:layout_width ="fill_parent"
15 android:layout_height ="wrap_content"
16 android:text ="@string/location" />
strings.xml:
1
<?
xml version="1.0" encoding="utf-8"
?>
2 < resources >
3
4 < string name ="hello" >Hello World, MainActivity! </ string >
5 < string name ="app_name" >04_location01 </ string >
6 < string name ="location" >绑定监听器 </ string >
7
8 </ resources >
2 < resources >
3
4 < string name ="hello" >Hello World, MainActivity! </ string >
5 < string name ="app_name" >04_location01 </ string >
6 < string name ="location" >绑定监听器 </ string >
7
8 </ resources >
AndroidManifest,xml:
1
<?
xml version="1.0" encoding="utf-8"
?>
2 < manifest xmlns:android ="http://schemas.android.com/apk/res/android"
3 package ="gaolei.location01"
4 android:versionCode ="1"
5 android:versionName ="1.0" >
6
7 < uses-sdk android:minSdkVersion ="8" />
8 < uses-permission android:name ="android.permission.ACCESS_FINE_LOCATION" />
9 < application
10 android:icon ="@drawable/ic_launcher"
11 android:label ="@string/app_name" >
12 < activity
13 android:name =".MainActivity"
14 android:label ="@string/app_name" >
15 < intent-filter >
16 < action android:name ="android.intent.action.MAIN" />
17
18 < category android:name ="android.intent.category.LAUNCHER" />
19 </ intent-filter >
20 </ activity >
21 </ application >
22
23 </ manifest >
2 < manifest xmlns:android ="http://schemas.android.com/apk/res/android"
3 package ="gaolei.location01"
4 android:versionCode ="1"
5 android:versionName ="1.0" >
6
7 < uses-sdk android:minSdkVersion ="8" />
8 < uses-permission android:name ="android.permission.ACCESS_FINE_LOCATION" />
9 < application
10 android:icon ="@drawable/ic_launcher"
11 android:label ="@string/app_name" >
12 < activity
13 android:name =".MainActivity"
14 android:label ="@string/app_name" >
15 < intent-filter >
16 < action android:name ="android.intent.action.MAIN" />
17
18 < category android:name ="android.intent.category.LAUNCHER" />
19 </ intent-filter >
20 </ activity >
21 </ application >
22
23 </ manifest >
MainActivity:
1
package gaolei.location01;
2
3 import android.app.Activity;
4 import android.content.Context;
5 import android.location.Location;
6 import android.location.LocationListener;
7 import android.location.LocationManager;
8 import android.os.Bundle;
9 import android.view.View;
10 import android.view.View.OnClickListener;
11 import android.widget.Button;
12
13 public class MainActivity extends Activity {
14 private Button button = null;
15
16 @Override
17 public void onCreate(Bundle savedInstanceState) {
18 super.onCreate(savedInstanceState);
19 setContentView(R.layout.main);
20
21 button = (Button) findViewById(R.id.locationButtonId);
22
23 button.setOnClickListener( new ButtonListener());
24 }
25
26 private class ButtonListener implements OnClickListener {
27
28 @Override
29 public void onClick(View v) {
30 // 得到locationManager对象
31 LocationManager locationManager = (LocationManager) MainActivity. this
32 .getSystemService(Context.LOCATION_SERVICE);
33 locationManager.requestLocationUpdates(
34 LocationManager.GPS_PROVIDER, 0, 0,
35 new TestLocationListener());
36
37 }
38 }
39
40 private class TestLocationListener implements LocationListener {
41
42 @Override
43 public void onLocationChanged(Location location) {
44 // TODO Auto-generated method stub
45 System.out.println(location.getLatitude());
46 System.out.println(location.getLongitude());
47
48 }
49
50 @Override
51 public void onProviderDisabled(String provider) {
52 // TODO Auto-generated method stub
53
54 }
55
56 @Override
57 public void onProviderEnabled(String provider) {
58 // TODO Auto-generated method stub
59
60 }
61
62 @Override
63 public void onStatusChanged(String provider, int status, Bundle extras) {
64 // TODO Auto-generated method stub
65
66 }
67
68 }
69 }
2
3 import android.app.Activity;
4 import android.content.Context;
5 import android.location.Location;
6 import android.location.LocationListener;
7 import android.location.LocationManager;
8 import android.os.Bundle;
9 import android.view.View;
10 import android.view.View.OnClickListener;
11 import android.widget.Button;
12
13 public class MainActivity extends Activity {
14 private Button button = null;
15
16 @Override
17 public void onCreate(Bundle savedInstanceState) {
18 super.onCreate(savedInstanceState);
19 setContentView(R.layout.main);
20
21 button = (Button) findViewById(R.id.locationButtonId);
22
23 button.setOnClickListener( new ButtonListener());
24 }
25
26 private class ButtonListener implements OnClickListener {
27
28 @Override
29 public void onClick(View v) {
30 // 得到locationManager对象
31 LocationManager locationManager = (LocationManager) MainActivity. this
32 .getSystemService(Context.LOCATION_SERVICE);
33 locationManager.requestLocationUpdates(
34 LocationManager.GPS_PROVIDER, 0, 0,
35 new TestLocationListener());
36
37 }
38 }
39
40 private class TestLocationListener implements LocationListener {
41
42 @Override
43 public void onLocationChanged(Location location) {
44 // TODO Auto-generated method stub
45 System.out.println(location.getLatitude());
46 System.out.println(location.getLongitude());
47
48 }
49
50 @Override
51 public void onProviderDisabled(String provider) {
52 // TODO Auto-generated method stub
53
54 }
55
56 @Override
57 public void onProviderEnabled(String provider) {
58 // TODO Auto-generated method stub
59
60 }
61
62 @Override
63 public void onStatusChanged(String provider, int status, Bundle extras) {
64 // TODO Auto-generated method stub
65
66 }
67
68 }
69 }